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


        }
    }

  

posted @ 2020-05-04 14:04  devs  阅读(571)  评论(0编辑  收藏  举报