Can I cast an object from type object to MyType<T> using reflection if I don’t know what T is until runtime?
Can I cast an object from type object to MyType<T> using reflection if I
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.
You can’t cast to a type unknown at compile time. Casting is really only useful as a compile-time construct, as you’d need to know the type in order to use it directly.
If your goal is to work with the object via Reflection, however, that’s a different scenario. In that case, you can use Type.MakeGenericType to create the correct type for your object.
This will allow you to use reflection to work upon your object.