How the "eat" operator works inside

I want to compare the type of an object with a type to see if they match. I don't have an object, just an object type.

I can do type1 == type2 and get general equality

I can have a recursive loop where I repeat the above step for type1.BaseType until BaseType is zero.

I can do type1.GetInterface( type2.FullName ) != null to check if type2 is an interface of type1

If I put everything together, I get

 if ( type2.IsInterface ) return type1.GetInterface( type2.FullName ) != null; while ( type1 != null ) { if ( type1 == type2 ) return true; type1 = type1.BaseType; } return false; 

This is all the is keyword. I can’t find a suitable keyword to connect to the Reflector search to find the function, and google search on β€œis” was not really useful

+6
source share
1 answer

is (Β§14.9.10 standard ) usually uses isinst , but is not necessary if the compile-time type is compatible with certain conversions.

Equivalent (in reverse order) with an object of type IsAssignableFrom . All this is true:

 "foo" is String; "foo" is object; typeof(String).IsAssignableFrom("foo".GetType()); typeof(object).IsAssignableFrom("foo".GetType()); 
+6
source share

All Articles