C# IsAssignableFrom & IsInstanceOfType & IsSubclassOf & Is

直观测试代码如下:

    public partial class NaviControl : UserControl, ITestControl
    {
        public NaviControl()
        {
            InitializeComponent();
            Load += NaviControl_Load;
        }

        private void NaviControl_Load(object sender, EventArgs e)
        {
            Debug.Print($"Base.IsAssignableFrom:");
            Debug.Print($"{typeof(UserControl).IsAssignableFrom(this.GetType())}");//True
            Debug.Print($"{typeof(ITestControl).IsAssignableFrom(this.GetType())}");//True
            Debug.Print($"This.IsAssignableFrom:");
            Debug.Print($"{this.GetType().IsAssignableFrom(typeof(UserControl))}");//False
            Debug.Print($"{this.GetType().IsAssignableFrom(typeof(ITestControl))}");//False
            Debug.Print($"Base.IsInstanceOfType:");
            Debug.Print($"{typeof(UserControl).IsInstanceOfType(this)}");//True
            Debug.Print($"{typeof(ITestControl).IsInstanceOfType(this)}");//True
            Debug.Print($"This.IsSubclassOf:");
            Debug.Print($"{this.GetType().IsSubclassOf(typeof(UserControl))}");//True
            Debug.Print($"{this.GetType().IsSubclassOf(typeof(ITestControl))}");//False
            Debug.Print($"This.Is:");
            Debug.Print($"{(this is UserControl)}");//True
            Debug.Print($"{(this is ITestControl)}");//True


        }
    }

  

C# IsAssignableFrom & IsInstanceOfType & IsSubclassOf & Is

上一篇:安卓漏洞悬赏飙至20万美元:竟是中国人出的招


下一篇:《代码大全》学习笔记(6):模块化设计