0

I have a generic class ValueCriterion that take another class type as parameter. Following code works fine.

new ValueCriterion<,CrashDetected>(0));

Where CrashDetected is a class.

But when I get the type of the CrashDetected class using the following code.

Type myType = Type.GetType("NetworkDB." + "CrashDetected");

and pass myType to ValueCriterion, the compiler gives me the following error:

new ValueCriterion<,myType>(0));

Error 1 Using the generic type requires 1 type arguments.

I will highly appreciate any help.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
Tom
  • 61
  • 3
  • 9

1 Answers1

2

You can use MakeGenericType method to create a generic type using a Type instance:

var type = typeof(ValueCriterion<>).MakeGenericType(myType);

var instance = Activator.CreateInstance(type, 0);
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • Selman22... you are great. My problem is half solved now. So I can create object and pass them. How about If i need to pass type as paramenter Execution.Wait – Tom Aug 11 '14 at 15:13
  • @Tom you should get the generic method Wait with Reflection then use MakeGenericMethod and pass the type. – Selman Genç Aug 11 '14 at 16:23