Thomas
Thomas

Reputation: 12107

Reflection, generics and multiple assemblies

I'm trying to solve this:

Type.GetType("Class1'[[Class2]]")

where Class1 and Class2 are in different assemblies.

I can parse the assemblies and find the Class1 type as well as the Class2 type, but how do I get to the Class1<Class2> type?

Upvotes: 0

Views: 84

Answers (2)

Arturo Martinez
Arturo Martinez

Reputation: 2583

if you can find the types all you need to is:

Type class1Type = assembly1.GetType("Class1"); //or however you are able to get this type
Type class2Type = assembly2.GetType("Class2"); //or however you are able to get this type
Type genericType = class1Type.MakeGenericType(class2Type);

genericType will be like having typeof(Class1<Class2>)

Upvotes: 2

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174309

I think, it should look like this:

Type.GetType("Class1`1[Class2]");

Note: I changed the apostroph from ' to ` and added the number of generic arguments.

If this is not enough, try specifying the classes including namespace and assembly:

Type.GetType("Namespace1.Class1`1[[Namespace2.Class2, Assembly2]], Assembly1");

Upvotes: 1

Related Questions