类是否继承某个类
// IsSubclassOf TestChild是否为TestParent的子类
typeof(TestChild).IsSubclassOf(typeof(TestParent))
// IsAssignableFrom TestParent是否为TestChild的父类
typeof(TestParent).IsAssignableFrom(typeof(TestChild))
类是否实现某个接口
typeof(TestClass).GetInterface(typeof(InterfaceClass).Name)
typeof(TestClass).GetInterface("InterfaceClass")
Demo
private void Print(Type typea, Type typeb)
{
if (typeb.IsInterface)
{
// 是否实现了接口判断
Console.WriteLine(string.Format("{0}实现了接口{1} {0} GetInterface {1} ====> {2}",
typea.Name,
typeb.Name,
typea.GetInterface(typeb.Name) != null
));
}
else
{
Console.WriteLine(string.Format("{0}是{1}的子类 {0} IsSubclassOf {1} ====> {2}",
typea.Name,
typeb.Name,
typea.IsSubclassOf(typeb)
));
Console.WriteLine(string.Format("{0}是{1}的父类 {0} IsAssignableFrom {1} ====> {2}",
typeb.Name,
typea.Name,
typeb.IsAssignableFrom(typea)
));
}
}