2011-10-21 48 views

回答

1

這似乎類似於:How To Detect If Type is Another Generic Type

public static bool IsAssignableToGenericType(Type givenType, Type genericType) { 
var interfaceTypes = givenType.GetInterfaces(); 

foreach (var it in interfaceTypes) 
    if (it.IsGenericType) 
     if (it.GetGenericTypeDefinition() == genericType) return true; 

Type baseType = givenType.BaseType; 
if (baseType == null) return false; 

return baseType.IsGenericType && 
    baseType.GetGenericTypeDefinition() == genericType || 
    IsAssignableToGenericType(baseType, genericType); 

}

+0

它的工作原理!謝謝 ! – BrijenVed

相關問題