I need to pass a generic type parameter to an interface. I have a string with the name of the type.
I have something like this:
string type = "ClassType";
Type t = Type.GetType("ClassType");
IProvider<t> provider = (IProvider<t>)someObject;
This doesn't work for me. What is the correct way to do it? Thanks.
What' you're trying to do is not really possible in the C# (and CLR) version of generics. When specifying a generic parameter it must be either ...
This information must be bound in the metadata of the assembly. There is no way to express a type name from string in metadata in this fashion.
It is possible to bind a generic at runtime based on string names but this requires reflection.
I believe this is what you are looking for =>Type.MakeGenericType
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With