2 回答

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超9個(gè)贊
下面的函數(shù)檢查您的類(lèi)的子類(lèi)是否提供了相同類(lèi)型的類(lèi)。如果類(lèi)型是泛型,則在泛型類(lèi)型定義上執(zhí)行檢查操作。
方法使用
bool isInherited = CheckIsDirectlyInherited(typeof(TestAbstract), new[] {typeof(SecondLevelAbstractClass), typeof(FirstLevelAbstract)});
方法
bool CheckIsDirectlyInherited(Type obj, Type[] baseTypes)
{
if (obj.BaseType == null)
return false;
var objGenericDefinition = obj.BaseType;
if (objGenericDefinition.IsGenericType)
{
objGenericDefinition = objGenericDefinition.GetGenericTypeDefinition();
}
foreach (Type baseType in baseTypes)
{
var baseTypeDefinition = baseType;
if (baseTypeDefinition.IsGenericType)
baseTypeDefinition = baseType.GetGenericTypeDefinition();
if (objGenericDefinition == baseTypeDefinition)
return true;
}
return false;
}

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超3個(gè)贊
是 HtSecurePage 的直接子類(lèi)
我想你已經(jīng)知道怎么做了
Console.WriteLine(typeof(HtSecureInstancePage<ZlsManager>).BaseType == typeof(HtSecurePage));
是 HtSecureInstancePage<> 的直接子類(lèi)
要檢查它,您可以使用以下內(nèi)容:
static bool IsDirectSubclassOfRawGeneric(Type parent, Type toCheck)
{
return toCheck.BaseType.IsGenericType && parent == toCheck.BaseType.GetGenericTypeDefinition();
}
...
Console.WriteLine(IsDirectSubclassOfRawGeneric(typeof(HtSecureInstancePage<>), typeof(InstancePage)));
- 2 回答
- 0 關(guān)注
- 175 瀏覽
添加回答
舉報(bào)