I know of is
and as
for instanceof
, but what about the reflective isInstance() method?
views:
1383answers:
4
+6
A:
bool result = obj.GetType().IsAssignableFrom(otherObj.GetType());
Konrad Rudolph
2008-11-11 23:13:08
Note IsAssignableFrom takes a Type, not an object, so you need to actually do OtherObj.getType().
FlySwat
2008-11-11 23:25:22
Thanks Jon – and remember, this is a wiki! I don't resent people correcting my mistakes.
Konrad Rudolph
2008-11-11 23:34:23
interesting... in java, the JVM treats "instanceof" specially, apparently its very very fast, which may explain why its unusually a keyword (there is also an isAssignable method in java).
Michael Neale
2008-11-11 23:37:29
+1
A:
just off the top of my head, you could also do:
bool result = ((obj as MyClass) != null)
Not sure which would perform better. I'll leave it up to someone else to benchmark :)
rally25rs
2008-11-12 01:10:18