I'm looking at this piece of code written by someone else, and I'm wondering when it would evaluate to true. Basically, it is saying someType is an instance of someOtherType. Does it even make sense? So far, I've tried:
derivedClass.GetType().IsInstanceOfType(typeof(BaseClass))
baseClass.GetType().IsInstanceOfType(typeof(DerivedClass))
myClass.GetType().IsInstanceOfType(typeof(MyClass))
And all of them evaluate to false.
Any help is appreciated.
Each of those 3 lines will return true only if the object involved (derivedClass
, baseClass
and myClass
respectively) is an instance of object
, or of the undocumented RuntimeType
object (note that Type is abstract), so for example the following would result in true statements:
var myObject = new object();
myObject.GetType().IsInstanceOfType(typeof(Console));
myObject = typeof(Object);
myObject.GetType().IsInstanceOfType(typeof(Console));
Note that the type used (in this case Console
) doesn't matter and has no effect on the outcome of the statement.
Why?
The documentation for IsInstanceOfType tells us that it will return true if the object passed in is an instance of current type, so for example the following statement will return true if myForm
is a class that derives from Form
, otherwise it will return false.
typeof(Form).IsInstanceOfType(myForm);
In your case myForm
is in fact typeof(BaseClass)
, which is of the undocumented type RuntimeType
(which derives from Type
), and so you are only going to get true
returned if this undocumented type happens to derive from the provided type - this is unlikely to be the desired behaviour.
What should I use instead?
What you are probably after is the is keword, which returs true if the provided object is an instance of the given type
derivedClass is BaseClass
baseClass is DerivedClass
myClass is MyClass
IsInstanceOfType()
checks whether the instance that you pass it is an instance of the type you called it on.
You're passing a System.Type
instance to IsInstanceOfType()
. That will only be true if you call it on typeof(Type)
or one of its base classes.
The Type.IsInstanceOf
documentation says that Type.IsInstanceOfType(o)
return "true if the current Type is in the inheritance hierarchy of the object represented by o, or if the current Type is an interface that o supports."
In the above examples, the Type
is the type returned by GetType()
, so it is the type of the object at the far left. The object o
is typeof(BaseClass)
, which is of type Type
. The current Type
is DerivedClass
and the object o
is typeof(BaseClass)
. The type of o
is Type
.
Plug that into the documentation. It returns "true if DerivedClass
is in the inheritance hierarchy of Type
."
This is rarely true.
The author almost certainly intended typeof(BaseClass).IsInstanceOfType(derivedClass)
, which is much more easily written as derivedClass is BaseClass
.
It is true when the object you call GetType() on is an instance of the System.Type object passed as an argument to the parameter of the IsInstanceOfType() method.
IsInstanceOfType
Determines whether the specified object is an instance of the current Type.
Sample : IsInstanceOfType(o)
true if the current Type is in the inheritance hierarchy of the object represented by o, or if the current Type is an interface supported by o. false if none of these conditions is not met, or if o is null, or if the current Type is an open generic type
来源:https://stackoverflow.com/questions/12160460/when-is-obj-gettype-isinstanceoftypetypeofmyclass-true