Is there an inheritance relationship between two classes in C#?

IsAssignableFrom: Determines whether an instance of the specified type can be assigned to an instance of the current type

B inherits from 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 { }


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324814911&siteId=291194637