C# 两个类是否继承关系

时间:2022-11-16 23:26:13

IsAssignableFrom:确定指定类型的实例是否可以分配给当前类型的实例

B继承自A

static void Main(string[] args)
{
Type a = typeof(A);
Type b = typeof(B);
Console.WriteLine(a.IsAssignableFrom(b)); //true
Console.WriteLine(b.IsAssignableFrom(a)); //false Type c = typeof(Nullable<int>);
Type d = typeof(int);
Console.WriteLine(c.IsAssignableFrom(d)); //true
Console.WriteLine(d.IsAssignableFrom(c)); //false Console.ReadLine();
} class A { } class B : A { }