• 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
    
    
            }
        }
    

      

  • 相关阅读:
    博客样式备份
    2018年终总结
    技术博客的太监
    LeetCode 日常填坑
    互联网之父
    TotoiseSVN的使用方法
    常用CMD命令
    量化策略
    浏览器加载js的阻塞与非阻塞
    Vue核心之数据劫持
  • 原文地址:https://www.cnblogs.com/honk/p/12826404.html
Copyright © 2020-2023  润新知