我想比较一个对象的类型和另一个类型是否相同。我没有这个对象,只有对象的类型。
我可以使用 type1 == type2
来进行一般的相等性比较。
我可以使用递归循环来重复上述步骤,直到 BaseType 为 null 为止,以此来比较 type1.BaseType
和 type2
是否相同。
我可以使用 type1.GetInterface(type2.FullName) != null
来检查 type2 是否是 type1 的接口。
如果将它们结合起来,就可以得到:
if ( type2.IsInterface )
return type1.GetInterface( type2.FullName ) != null;
while ( type1 != null ) {
if ( type1 == type2 )
return true;
type1 = type1.BaseType;
}
return false;
“is”关键字只是这样的吗?我找不到正确的关键字来在Reflector搜索中找到该函数,而且在谷歌搜索“is”也没有什么帮助。