I have the System.Type of a certain object but need to pass that over as a Type Parameter T to another method… is that somehow possible? Or am I lost in the bigger picture there?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is not possible. If you think about it, a Type Parameter is resolved at compile time, whereas the System.Type is resolved by reflection at runtime.
Now, having said it’s impossible, it is possible by using reflection. If you create the class with reflection you can pass in a System.Type as a parameter, but it’s probably just worth redesigning whatever it is you’re trying to do.
EDIT: Here’s some ideas for a redesign.
Where does the System.Type come from? Could you pass it in as a type parameter itself so it can be passed through?
If not, could you make an adapter that handles the known types that will be used? Perhaps a switch statement that converts from the System.Type to the right sort of generic call? Anything is faster than reflection.