I have a Type
, which I parsed from it's fully qualified name:
Type paramType = Type.GetType(param.TypeName);
How can I use paramType
to create an instance of an object with paramType
as a type parameter?
I have a Type
, which I parsed from it's fully qualified name:
Type paramType = Type.GetType(param.TypeName);
How can I use paramType
to create an instance of an object with paramType
as a type parameter?
It's simple:
void Main()
{
Type typeParam = typeof(int);
Type typeGeneric = typeof(Foo<>).MakeGenericType(typeParam);
object foo = Activator.CreateInstance(typeGeneric);
//foo is a Foo<int>
}
public class Foo<T>
{
}