1

I have 3 types all from diferent dlls y can't find how to store in a string the generic type and how to create the instance:

Type type1 = GetType1();
Type type2 = GetType2();
string strClassGenericType = "Namespace.ClassName<,>, DllName";

Type template = // How to get the generic template type?

Type genericType = template.MakeGenericType(new[] { type1, type2 });
object instance = Activator.CreateInstance(genericType);

Im not sure if this fits to my requeriment.

Community
  • 1
  • 1
Najera
  • 2,869
  • 3
  • 28
  • 52

4 Answers4

2

In the particular case where you want the type of object that does not have it's type parameters known, you can use a backtick with the number of type parameters. Here is an example with Tuple:

Console.WriteLine(typeof (Tuple<,>).FullName); //For information only, outputs "System.Tuple`2"
var typeName = "System.Tuple`2";
var type = Type.GetType(typeName);
var generic = type.MakeGenericType(typeof (string), typeof (int));
Console.WriteLine(generic.FullName); //Outputs the type with the type parameters.
vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • 1
    or `Console.WriteLine(typeof(Tuple<,>).AssemblyQualifiedName)` for more information and better understanding. – abatishchev Mar 30 '15 at 21:37
1

The correct string representation of a generic type is like this:

"System.Collections.Generic.Dictionary`2[[System.String],[System.Object]]"

Where '2 means the number of generic type parameters.

For the fully qualified type name see the fiddle.


If you're looking for a generic type having no generic type parameters specified (so called open generic) then it's this:

"System.Collections.Generic.Dictionary`2, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • I think he is asking how to take a type name that does not have the type parameters specified at all, like `Dictionary\`2`, then at runtime specify the type parameters with `MakeGenericType`. – vcsjones Mar 30 '15 at 21:34
  • Thanks for the info, this is more useful for my requirement, but vcsjones answer is little more didactic. – Najera Mar 30 '15 at 22:41
1

Both vcsjones and abatishchev has a correct answer, but you miss about the dlls.

Thanks to vcsjones i used this:

string strGenericType = "Namespace.ClassName`2, DllName";
Type template = Type.GetType(strGenericType);

Type genericType = template.MakeGenericType(new[] { type1, type2 });
object instance = Activator.CreateInstance(genericType);

Thanks to abatishchev this is shorter:

string strGenericType = "Namespace.ClassName`2[[Namespace1.Type1, Type1DllName],[Namespace2.Type2, Type2DllName]], DllName";
Type genericType = Type.GetType(strGenericType);

object instance = Activator.CreateInstance(genericType);
Najera
  • 2,869
  • 3
  • 28
  • 52
0

I take it you are trying to create a type from a string value.

Try:

Type type = Type.GetType("System.String");

eg to create a textbox type from a string:

Type.GetType("System.Windows.Forms.TextBox, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

See MSDN.

Sevle
  • 3,109
  • 2
  • 19
  • 31
Xela
  • 2,322
  • 1
  • 17
  • 32