IsSubclassOf用于判断class之间是否继承。接口之间的继承无法进行判断。
接口继承测试 public interface ID { } public interface IE : ID { } public class ClassD : ID { } Console.WriteLine(typeof(ClassD).IsSubclassOf(typeof(ID))); // FALSE Console.WriteLine(typeof(IE).IsSubclassOf(typeof(ID)); // FALSE 泛型类测试 public class ClassF<T> { } public class ClassG<T> : ClassF<T> { } public class ClassH : ClassF<int> { } // no type constraint, no good Console.WriteLine(typeof(ClassG<>).IsSubclassOf(typeof(ClassF<>))); //FALSE // no covariance support here Console.WriteLine(typeof(string).IsSubclassOf(typeof(object))); // TRUE Console.WriteLine(typeof(ClassG<string>).IsSubclassOf(typeof(ClassF<object>))); // FALSE // type constraint has to match Console.WriteLine(typeof(ClassG<int>).IsSubclassOf(typeof(ClassF<int>)));// TRUE Console.WriteLine(typeof(ClassH).IsSubclassOf(typeof(ClassF<int>))); // TRUE Console.WriteLine(typeof(ClassH).IsSubclassOf(typeof(ClassF<long>))); // FALSE特别注意泛型类的判断
用于确定是否可以从指定Type的实例分配当前Type的实例。也就是说用于判断继承和实现接口类。
泛型测试留给园友们自行测试。