How to determine if type is RunTimeType?

How to determine type of type RunTimeType? It works for me, but it's kind of kludgy:

    private bool IsTypeOfType(Type type)
    {
        return type.FullName ==  "System.RuntimeType";
    }
+2
source share
2 answers

I assume that you really want to know if the object describes the Typeclass Type, but the object Typeis - typeof(RuntimeType)and not typeof(Type), and therefore comparing it typeof(Type)with fails.

What you can do is check if an instance of the type described by the object can be a type Typevariable Type. This gives the desired result, because it RuntimeTypecomes from Type:

private bool IsTypeOfType(Type type)
{
    return typeof(Type).IsAssignableFrom(type);
}

Type, Type, GetType:

private bool IsRuntimeType(Type type)
{
    return type == typeof(Type).GetType();
}

, typeof(Type) != typeof(Type).GetType(), .


:

IsTypeOfType(typeof(Type))                          // true
IsTypeOfType(typeof(Type).GetType())                // true
IsTypeOfType(typeof(string))                        // false
IsTypeOfType(typeof(int))                           // false

IsRuntimeType(typeof(Type))                         // false
IsRuntimeType(typeof(Type).GetType())               // true
IsRuntimeType(typeof(string))                       // false
IsRuntimeType(typeof(int))                          // false
+6
return type == typeof(MyObjectType) || isoftype(type.BaseType) ;
-1

All Articles