Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing typeof(Type) to System.RuntimeType

Tags:

c#

First off, I do have a workaround for this issue (use type.FullName), so its just for interest sake really.

*Edited for clarification; This is really just a question of what is the best way to compare the type of Type in code.

object o;
Type t = typeof(someclass);
o = t;

// Cant do this
if (o.GetType() == typeof(RuntimeType)) {
}

The object being attached to o can be anything, including types. I'm examining the type of the object to see how to deal with it further. So if it was a string, I might do one thing, if its an enum something else, if its a type, something else again. I'm basically dealing with the same sort of thing as String.Format("",x,y,z) where the parameters are all completely arbitrary.

I could write

if (o.GetType().FullName == "System.RuntimeType") {} or
if (o.GetType()== typeof(Type).GetType()) {}

But both are pretty ugly looking (though it works).

Original Question:

Apologies if this has been asked before, but I cant find an exact match (there are a lot of how do I get a type of object, object is class, or object.GetType() style questions.)

This question came close, but its not quite the same since I cant avoid calling GetType on a type(I don't think? Hope I'm not overlooking something really simple or cheaty...); What's the difference between System.Type and System.RuntimeType in C#?

So basically, I've created an attribute with an arbitrary number of parameters. These can be of any object, including types (I use the type to decide how the property the attribute is attached to should be handled). For example, while the property could be an integer, this integer is the primary key in a database for some particular table. If I assign this type to the attribute, I can write generic code to deal with any kind of object without having to write a ton of special case code. That said, I could use a string, or any other value like an enum, but since the models already exist there didnt seem to be any point in not using them since I can just create instances of them with Activator.CreateContext() based on the type being passed in.

[AttributeUsage(AttributeTargets.Property)]
public class SomeAttribute: System.Attribute
{
    public SomeAttribute(params object[] parameters)
    {
        foreach(var p in parameters) {
            ...
            // Type of Type.  Uh oh.
            Type t = p.GetType();

            // Evaluates as false when t is of type Type(its actually RuntimeType, with is protected). Sad face.
            if (t == typeof(Type)) {
                ...
            }
            ...
        }
    }
}

And I've slapped this attribute on some properties;

public class someclass
{
    [SomeAttribute(..., typeof(someotherclass))
    public sometype someproperty { get; set; }
}

When the program gets to

if (t == typeof(Type))

if always return false. The type of t is evaluated as System.RuntimeType as opposed to System.Type. Unfortunately I cant just change that to

if (t == typeof(RuntimeType))

Since I get a compiler error of "'RuntimeType' is inaccessible due to its protection level".

Is there any way to perform a type match on RuntimeType other than by looking at the type.Name or type.FullName?

*reedited for more clarification.

like image 583
There and That Avatar asked Oct 31 '25 06:10

There and That


1 Answers

Consider Type is a normal class.

Type t1 = typeof(string);
Type t2 = "1".GetType();

There's no difference between t1 and t2. And What is t1? Just consider t1 or t2 as a normal object and the type of the object is System.Type. It means that t1 or t2 nearly equals new Type("System.String").

How can I know an object obj is aStringBuilder?

Just use is or as:

bool objIsStringBuilder = obj is StringBuilder;

How can I know t1 is a Type?

In the same way:

bool t1IsType = t1 is Type;

What is typeof(Type) or t1.GetType()?

Type t3 = typeof(Type);

Then t3 is nearly equals new Type("System.Type"). So of course t1 and t3 are not equal.

like image 138
skyoxZ Avatar answered Nov 02 '25 20:11

skyoxZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!