samedi 24 octobre 2015

Do all Type classes inherit from RuntimeType?

I want to know whether a on object of type Type is actually a type for a normal class (e.g. Object, Int32, etc.) or a type for a meta-object (e.g. typeof(Object), typeof(Int32), etc.).

By type of an object I mean:

Type t = typeof(Object);
Console.WriteLine("Type is {0}", t.FullName);

Type is System.Object

And by type of a type object, i.e. meta-object:

Type t = typeof(Object).GetType();
Console.WriteLine("Type is {0}", t.FullName);

Type is System.RuntimeType

I couldn't find any method/property in Type or TypeInfo to tell whether the object for which a Type object is made is actually a Type rather than a normal object.

If I have the object, I could do that:

bool IsType(object o) { return o is Type; }

But, I don't have the object itself, only its type.

I was hoping for something along the lines:

bool IsType(Type t) { return t.GetTypeInfo().IsType; }

But there's nothing like it, it seems..

So the only thing I can think of so far is:

bool IsType(Type type)
{
    // Can't use typeof(RuntimeType) due to its protection level
    Type runtimeType = typeof(Object).GetType();
    return runtimeType.Equals(type);
}

Yet, I can't be sure that GetType() for all objects of type Type would return RuntimeType, nor that they actually inherit from it...





Aucun commentaire:

Enregistrer un commentaire