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.