3

Consider following line of code :

classA<classB> C;

I want to create instance of C during run-time by using System.Reflection instead. For that I will need to know Type of classA and classB during run-time.

I know how to get type of classB

Type classBType = AssemblyContaingClassB.GetType("namespace.classB");

Q1> How can I use this classBType to get type of classA ?

Q2> I understand that to create instance in run-time, we call following line of code:

object C = Activator.CreateInstance(TypeName);

To create instance of C, What TypeName should above code use? will Type of classA sufficient to create this instance

Thanks in advance and Apology if this question is too basic. I am new to c#.

EDIT:

The problem is Fixed. From p.s.w.g's answer, there was problem in getting type of class with generic parameter. The solution on how to get type of generic parameter can be found here. After that I just did what p.s.w.g's answer suggest and its work.

Community
  • 1
  • 1
someone_ smiley
  • 1,006
  • 3
  • 23
  • 42

2 Answers2

8

You could use the MakeGenericType method on the open-generic classA<> type:

Type classBType = ... get from somewhere at runtime
Type classAType = typeof(classA<>).MakeGenericType(classBType);
object classAInstance = Activator.CreateInstance(classAType);

The MakeGenericType will close the classA<> generic and make it possible to instantiate.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
4

Given your latest update it seems like the issue is loading the type classA<> at run-time. To avoid naming conflicts the C# compiler generates a name like SimpleClassName`N where N is the number of generic type parameters, so to get the classA<> type through reflection, use the name namespace.classA`1. After that, you would simply use the MakeGenericType method:

var classAType = AssemblyContaingClassA.GetType("namespace.classA`1");
var classBType = AssemblyContaingClassB.GetType("namespace.classB");
var genericType = classAType.MakeGenericType(classBType);

object instance = Activator.CreateInstance(genericType);

Of course, unless you have to load the types at run-time, it's easier and safer to get the types at compile-time using the typeof operator:

var classAType = typeof(classA<>);
var classBType = typeof(classB);
var genericType = classAType.MakeGenericType(classBType);

object instance = Activator.CreateInstance(genericType);
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • it strikes me that, if you're in a position where you could write the second form of your code, you could just as easily have `new classA();` and be done with it - or is there some circumstance that I'm not thinking of here? – Damien_The_Unbeliever Jul 11 '13 at 06:12
  • @Damien_The_Unbeliever It could be the case that you have to write them separately. e.g. `Type MethodA() { return typeof(classA<>); }`, `Type MethodB() { return typeof(classB); }`, and `Type MethodC() { return MethodA().MakeGenericType(MethodB()); }` – p.s.w.g Jul 11 '13 at 06:19
  • I got value of classAType = null. Do you know how can i fix this? – someone_ smiley Jul 11 '13 at 07:27
  • It throw exception with message "Could not load type 'classA' from assembly '//assembly details//'" – someone_ smiley Jul 11 '13 at 08:08
  • I suggest that you edit the question on getting type of classAType : http://stackoverflow.com/questions/17589613/how-do-i-gettype-of-class-with-generic-parameter-in-run-time/17589713?noredirect=1#17589941 Then i can accept this post as accepted answer. – someone_ smiley Jul 11 '13 at 09:46
  • @someone_smiley You're right, the compiler munges generic class names a little bit. I've updated my answer to be more complete. – p.s.w.g Jul 11 '13 at 14:21