C#常见函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 反射常用函数
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Student是否能为Person类型赋值");
            bool bl = typeof(Person).IsAssignableFrom(typeof(Student));//True
            Console.WriteLine(bl);

            Student s = new Student();
            Person p = new Person();
            //IsInstanceOfType(object o)判断对象o是否为当前类的实例(当前类可以是o的类、父类、接口)
            //子类可赋值为父类bool bl1 = typeof(Person).IsInstanceOfType(s);//True
            //bool bl1 = typeof(I1).IsInstanceOfType(s);//True
            //bool bl1 = IsInstanceOfType(s);//True

            //判断当前类是否为类Person的子类
            //bool bl1 = s.GetType().IsSubclassOf(typeof(Person));

            //判断是否为抽象类
            bool bl1 = typeof(Person).IsAbstract;

            Console.WriteLine(bl1);

            Console.ReadKey();
        }
    }

    class Person
    { }

    class Student:Person,I1
    {

        public void Test()
        {
            throw new NotImplementedException();
        }
    }

    interface I1
    {
        void Test();
    }
}

原文地址:https://www.cnblogs.com/my-cat/p/7976641.html