1

I'm using reflection to dynamically create objects

var type = Type.GetType("Demo.Namespace.AnimalViewModel, Demo.Assembly", true);
var fooType = Type.GetType("Demo.Namespace.Foo, Demo.Assembly", true);
Activator.CreateInstance(type) as AnimalViewModel<fooType>;

Getting a compile error:

'fooType' is a variable but is used like a Type.

How can I set the type for AnimalViewModel , with the fooType variable? fooType -> Foo

Rahul
  • 2,431
  • 3
  • 35
  • 77
  • 3
    What do you expect to do with `obj` with your reflection version. show a example of the code you plan on using with it. – Scott Chamberlain May 17 '17 at 21:17
  • 1
    It might not be possible to do what you want. On the other hand, you may be able to accomplish the same thing by making your view model implement `IAnimalViewModel` and reference it through that type. – recursive May 17 '17 at 21:18
  • I'm a bit confused about the scenario though. If line 3 compiles, then `AnimalViewModel` is already statically available without using reflection. Why resort to it at all? – recursive May 17 '17 at 21:20
  • I have updated the question, just ignore the obj example. I deleted it. ~...as AnimalViewModel~ gives a compile error. That is my problem. – Rahul May 17 '17 at 21:22
  • 3
    You cannot use `fooType` in that manner. What you place inside of `< >` should be a type name, not a variable--doesn't matter if it's reflection or not. – Kenneth K. May 17 '17 at 21:26
  • @KennethK. Yes, but i only have type information. Is it possible to get the Type name from type variable? – Rahul May 17 '17 at 21:32
  • 1
    It seems like you're trying to have the flexibility of creating instances dynamically with the rigidity of type safety. The only way I know of to achieve this would be to define an interface (or more) that your relevant types implement. You can create the instance dynamically the same way you are doing now, but when it comes to casting you cast to the interface. Then in code you only ever reference the interface. – Kenneth K. May 17 '17 at 21:38
  • 1
    See also related: https://stackoverflow.com/questions/223952/create-an-instance-of-a-class-from-a-string, https://stackoverflow.com/questions/19555896/how-can-i-dynamically-instance-an-object, and https://stackoverflow.com/questions/326285/deciding-on-type-in-the-runtime-and-applying-it-in-generic-type-how-can-i-do-t. Note that creating the instance is one thing. Using it is something else entirely. Your question does not explain how, even once you have created this dynamically-specified type, how you think you're going to use it. – Peter Duniho May 17 '17 at 23:56

1 Answers1

1

Correct, the type info that goes inside <> is a compile time type reference. I.e. the type itself, not the variable that holds Type instance that describes the type. The reason is because type here is resolved and compile-time, not run-time.

Look at answer https://stackoverflow.com/a/2173115/1556108, specifically section:

Type unboundGenericList = typeof(List<>);
Type listOfInt = unboundGenericList.MakeGenericType(typeof(int));
if (listOfInt == typeof(List<int>))
    Console.WriteLine("Constructed a List<int> type.");

Because you're building your type during runtime, you need to use the same approach. Something along the lines of:

var type = Type.GetType("Demo.Namespace.AnimalViewModel, Demo.Assembly", true);
var fooType = Type.GetType("Demo.Namespace.Foo, Demo.Assembly", true);
var closedType = fooType.MakeGenericType(fooType)
var closedInstance = Activator.CreateInstance(closedType);

And you cannot really cast it to compile-time known type, unless you can guarantee that loaded type derives from the compile-time known type. Dynamic typing is exactly that - you handle it dynamically at runtime.

Community
  • 1
  • 1
LB2
  • 4,802
  • 19
  • 35