-1

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?

Arli Chokoev
  • 521
  • 6
  • 22

1 Answers1

1

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>
{
    
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172