I have a class:
public class MyClass<T>
I have a config file that I am reading that has, say, Guid
for the type T
.
How do I do this, except using the string from the config file instead of the actual type (instead of hard-coding Guid
)?
MyClass<Guid> = new MyClass<Guid>();
You need the fully qualified type name:
Then you can use reflection to get the parameterized type:
Which you can then instantiate via:
But I'm not sure why you would want to do this or why it would be useful in your scenario. Since using this technique, you'll never be able to interact with
MyClass<T>
whereT
is defined as something you're specifying at compile-time, you lose most of the advantages of using generics in the first place.