Greg Finzer
Greg Finzer

Reputation: 7054

How to determine if a Type is of RunTimeType?

How do I determine if a Type is of type RunTimeType? I have this working but it is kind of kludgy:

    private bool IsTypeOfType(Type type)
    {
        return type.FullName ==  "System.RuntimeType";
    }

Upvotes: 5

Views: 3522

Answers (3)

alelom
alelom

Reputation: 2983

Really, the only trouble is that System.RuntimeType is internal, so doing something simple like:

   if (obj is System.RuntimeType)

does not compile:

CS0122 'RuntimeType' is inaccessible due to its protection level.

So the solution from @dtb above is right. Expanding on their answer:

void Main()
{
    object obj = "";
    // obj = new {}; // also works

    // This works
    IsRuntimeType(obj.GetType()); // Rightly prints "it's a System.Type"
    IsRuntimeType(obj.GetType().GetType()); // Rightly prints "it's a System.RuntimeType"

    // This proves that @Hopeless' comment to the accepted answer from @dtb does not work
    IsWhatSystemType(obj.GetType()); // Should return "is a Type", but doesn't
    IsWhatSystemType(obj.GetType().GetType());
}

// This works
void IsRuntimeType(object obj)
{
    if (obj == typeof(Type).GetType())
        // Can't do `obj is System.RuntimeType` -- inaccessible due to its protection level
        Console.WriteLine("object is a System.RuntimeType");
    else if (obj is Type)
        Console.WriteLine("object is a System.Type");
}

// This proves that @Hopeless' comment to the accepted answer from @dtb does not work
void IsWhatSystemType(object obj)
{
    if (obj is TypeInfo)
        Console.WriteLine("object is a System.RuntimeType");
    else
        Console.WriteLine("object is a Type");
}

Working .NET fiddle here.

Upvotes: 0

dtb
dtb

Reputation: 217233

I guess that you actually want to know if a Type object describes the Type class, but the Type object is typeof(RuntimeType) and not typeof(Type) and so comparing it to typeof(Type) fails.

What you can do is check if a instance of the type described by the Type object could be assigned to a variable of type Type. This gives the desired result, because RuntimeType derives from Type:

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

If you really need to know the Type object that describes the Type class, you can use the GetType Method:

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

However, because typeof(Type) != typeof(Type).GetType(), you should avoid this.


Examples:

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

Upvotes: 10

Matthew Sanford
Matthew Sanford

Reputation: 1099

return type == typeof(MyObjectType) || isoftype(type.BaseType) ;

Upvotes: -1

Related Questions