typeof(TModels) returns RuntimeType

1.9k views Asked by At

I need a way of getting the "true-type" of an object or a class (in this case a generic-class).. for now all I get is a RuntimeType.. I have read all over the internet but I just cant find an answer that doesnt just return a RuntimeType..

2

There are 2 answers

0
phoog On

System.RuntimeType is an internal concrete class that implements the abstract System.Type class. See the accepted answer to What's the difference between System.Type and System.RuntimeType in C#? for a good discussion. So, you see, as svick said in his comment, RuntimeType is the true type.

You can't find a way that doesn't return a RuntimeType object, because GetType() always returns a RuntimeType object. You will never be able to get a Type object that isn't an instance of some more-derived class, because Type is abstract.

1
Kristof On

propertyinfo.PropertyType.GetGenericArguments() should give you a list of the generic arguments.
It is a list because a class can have more then 1 generic.
More info here :
http://msdn.microsoft.com/en-us/library/ms173128.aspx
and here
http://msdn.microsoft.com/en-us/library/b8ytshk6.aspx