I have a problem using dynamic types to instantiate a custom class. Example, I have the following class:
public class myClass<T>
{
public myClass(String header);
}
If I use the following code, everything works fine:
var myInstance = new myClass<int>("myHeader");
However, I am in a position I don't have the int
type defined, so I need to dynamically cast it from a generic type parameter. What I tried so far:
1.
Type myType = typeof(int);
var myInstance = new myClass<myType>("myHeader");
2.
int myInt = 0;
Type myType = myInt.GetType();
var myInstance = new myClass<myType>("myHeader");
In all examples I get the following error:
The type or namespace name 'myType' could not be found (are you missing a using directive or an assembly reference?)
The reasons I cannot use int
directly is because I am loading the types from specific assemblies at runtime, so they will not be "int" at all times.