1

I've seen many examples of this tool which abstracts away the cumbersome syntax of Reflection. However none demonstrate instantiation of an unknown type. Is it safe to assume this isn't possible with "dynamic"?

3
  • which tool are you talking about? Commented Jul 24, 2011 at 1:16
  • and how does it "abstract away the syntax of Reflection"? Commented Jul 24, 2011 at 1:19
  • 1
    One is able to assume the existence of properties and methods rather than use Reflection as the middle man. Commented Jul 24, 2011 at 1:26

3 Answers 3

4

Logically, it's impossible to instantiate an unknown type -- to instantiate a type, something must know what it is.

dynamic is useful for manipulating values of an unknown type (by assuming that it is capable of certain operations, which will fail at runtime if they are in fact not possible). To instantiate any type, however, you either need to use compile-time instantiation (e.g. using a C# constructor call), or else you need an instance of Type that corresponds to your desired type.

Sign up to request clarification or add additional context in comments.

3 Comments

I think this answer is a bit misleading... I would guess that the reasons you can't do something like "dynamic var = new SomeNamespace.SomeClass(...)", are due to implementation details in the compiler/runtime, rather than there being a fault in the general concept. I don't think there is anything stopping someone from writing a function like "Construct(params Object[] args)" that does what the OP is after here.
@Aaron: But you would still either need that Construct method to either be (1) parameterized by the desired type (meaning that the type must be known at compile time) or (2) take a Type object as a parameter, meaning that the type must be known at runtime, or (3) take a string which gets passed to Type.GetType (after which it's the same as #2). That's what I meant by "something must know what it is".
Yes, you're right. I had a typo above- I had intended Construct(string typeName, params Object[] args) as the signature. I can't speak for the OP, but I assumed he/she did know what type they wanted to instantiate, just like you would need to know the name of the method you want to call when you call a method dynamically.
2

The compiler can use the dynamic keyword so that the dlr will construct a type, but it's designed to late bind the arguments of a constructor rather than the type to be constructed. The opensource framework ImpromptuInterface abstracts the dlr calls, including the constructor. If you need to call a constructor that has arguments this will run about 5 times faster than using reflection/Activator.

var x = Impromptu.InvokeConstructor(Type.GetType("SomeType"),args...); 

Comments

1

I don't know what your goal is... but do you mean something like

dynamic X = Type.GetType("SomeUnknownType").GetConstructor(null).Invoke(null); 

?

the above just calls the default (parameterless) constructor of the Type "SomeUnknownType" and assign the resulting instance to a dynamic .

1 Comment

I mean something like "dynamic X = dynamic.GetInstance("SomeUnknownType");". And perhaps an overload that passes in constructor parameters.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.