Click here to Skip to main content
15,567,466 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a question about a method type like this:


my define T is a generic class name

I would like to pass my method name to this generic type.
When I get this element class name to mass my method it is in string type.
How can I pass this name as an acceptable generic type to satisfy my
customeName<T> type.

What I have tried:

convert some type as a 
<pre> object Te = Activator.CreateInstance(t);
Updated 2-Oct-20 22:47pm

1 solution

I'm pretty sure you can't: the system has to know the type of the generic (the "T" in the class definition) when you actually use it.

So you can't pass the name of a type to a generic class constructor and expect it to sort out for you exactly what type your class instance should be - you have to be explicit when you construct the instance. Remember that C# is strongly typed, so your idea would break that by weakly typing the declared instance variable.

I can't help thinking you have overthought this, and are trying to be too clever: I'd consider looking at what you are trying to do and why you think this is a good solution. You might also want to look at the dynamic keyword instead.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900