0

Short form: My question is similar to this other stackoverflow answer, except that I need the Type of 'dynamic' rather than string etc

Dynamically create an array of Type in C#

Basically, I'm running a query using Dapper, which supports object mapping up to 7 objects -- I need 8.

When doing 7 it looks like:

connection.Query<dynamic, dynamic, dynamic, dynamic, dynamic, dynamic, dynamic, dynamic>(sql, (obj1, obj2, obj3, obj4, obj5, obj6, obj7) => {...});

When doing 8 it's supposed to be:

connection.Query<dynamic>(sql, Type[], (objArray) => {...});

Where Type[] is an array of types -- in this case, 8 dynamic types.

I've tried all sorts of things from

var types = new Type[]{
    dynamic,
    dynamic,
    dynamic,
    dynamic,
    dynamic,
    dynamic,
    dynamic,
    dynamic
}

to

var types = new Type[]{
    Type.GetType("System.Dynamic.DynamicObject"),
    Type.GetType("System.Dynamic.DynamicObject"),
    Type.GetType("System.Dynamic.DynamicObject"),
    Type.GetType("System.Dynamic.DynamicObject"),
    Type.GetType("System.Dynamic.DynamicObject"),
    Type.GetType("System.Dynamic.DynamicObject"),
    Type.GetType("System.Dynamic.DynamicObject"),
    Type.GetType("System.Dynamic.DynamicObject")
}

And various others. None of which works.

I'm figuring since 'dynamic' is rather the absence of a type, that maybe I'm approaching this slightly wrong?

Anyway, any ideas?

5
  • 3
    Don't do that. What problem are you trying to solve? Commented May 11, 2016 at 18:59
  • Whence cometh IDbConnection.Query? This must be an extension method since the interface defines no such method, but from where? What technology are you using? Or is your problem that you want to write such a method? Commented May 11, 2016 at 18:59
  • dynamic is not a type - is just means that any binding will be done at run-time based on the actual type of the object. Can you add more about what you're trying to do? Commented May 11, 2016 at 18:59
  • Is this for a dapper call? if it is (as it seems to me) you got all worng, calrify it and I can post an example. Commented May 11, 2016 at 19:03
  • It is dapper. I'm maintaining existing code, so I can't say anything for the quality of what's already there. I'm primarily a node and rails guy, so this is rather unfamiliar to me. Commented May 11, 2016 at 19:10

1 Answer 1

1

C# Reference: dynamic

Type dynamic behaves like type object in most circumstances. However, operations that contain expressions of type dynamic are not resolved or type checked by the compiler. The compiler packages together information about the operation, and that information is later used to evaluate the operation at run time. As part of the process, variables of type dynamic are compiled into variables of type object. Therefore, type dynamic exists only at compile time, not at run time.

So you should just pass this :

var types = Enumerable.Repeat(typeof(object), 8).ToArray();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.