Net Core教程

C# 判断一个类是否继承指定类

本文主要是介绍C# 判断一个类是否继承指定类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.类结构

    /// <summary>
    /// 测试继承-父类
    /// </summary>
    public class TestInheritBase { }
    /// <summary>
    /// 测试继承-抽象父类
    /// </summary>
    public abstract class TestAbstractInheritBase { }
    /// <summary>
    /// 测试继承-接口
    /// </summary>
    public interface ITestInheritBase { }
    /// <summary>
    /// 测试继承-类
    /// </summary>
    public class TestInherit : TestInheritBase, ITestInheritBase
    {
        public TestInherit() { }
        public TestInherit(int num) { }
        public TestInherit(int num, string str) { }

        public int Num { get; set; }
        public string Str { get; set; }

        public void Run() { }
        public void Run(int num) { }
        public void Run(int num, string str) { }

    }
    /// <summary>
    /// 测试继承-抽象类
    /// </summary>
    public class TestAbstractInherit : TestAbstractInheritBase, ITestInheritBase { }

2.检查对象是否与给定类型兼容,关键字: is

            // 测试类继承
            var typeTestInherit = typeof(TestInherit);
            var typeTestInheritBase = typeof(TestInheritBase);
            var typeITestInheritBase = typeof(ITestInheritBase);
            // 测试抽象类的继承
            var typeTestAbstractInherit = typeof(TestAbstractInherit);
            var typeTestAbstractInheritBase = typeof(TestAbstractInheritBase);
            {
                var newTestInherit = new TestInherit();
                var newTestInheritBase = new TestInheritBase();

                _testOutputHelper.WriteLine($"TestInherit 继承 TestInheritBase:{newTestInherit is TestInheritBase}");// true
                _testOutputHelper.WriteLine($"TestInherit 继承 ITestInheritBase:{newTestInherit is ITestInheritBase}");// true
                _testOutputHelper.WriteLine($"TestInheritBase 继承 TestInheritBase:{newTestInheritBase is TestInheritBase}");// true
                _testOutputHelper.WriteLine($"TestInheritBase 继承 ITestInheritBase:{newTestInheritBase is ITestInheritBase}");// false
            }

3.确定当前 Type 是否派生自指定的 Type,关键字:IsSubclassOf 和 IsAssignableFrom

            {
                _testOutputHelper.WriteLine($"TestInherit 继承 TestInheritBase:{typeTestInherit.IsSubclassOf(typeTestInheritBase)}");// true
                _testOutputHelper.WriteLine($"TestInherit 继承 ITestInheritBase:{typeTestInherit.IsSubclassOf(typeITestInheritBase)}");// false
                _testOutputHelper.WriteLine($"TestAbstractInherit 继承 TestAbstractInheritBase:{typeTestAbstractInherit.IsSubclassOf(typeTestAbstractInheritBase)}");// true

                _testOutputHelper.WriteLine($"TestInherit 继承 TestInheritBase:{typeTestInheritBase.IsAssignableFrom(typeTestInherit)}");// true
                _testOutputHelper.WriteLine($"TestInherit 继承 ITestInheritBase:{typeITestInheritBase.IsAssignableFrom(typeTestInherit)}");// true
            }

3.1.IsSubclassOf 方法可以用来确定:一个类是否派生自另一个类|ValueType|Enum|委托;不能用于确定:接口是派生自另一个接口,还是类实现接口,详见文档

3.2.IsAssignableFrom 方法可以用来确定:接口是派生自另一个接口,还是类实现接口,详见文档

4.通过反射获取父类和继承的接口

            {
                // 父类
                var baseType = typeTestInherit.BaseType;
                _testOutputHelper.WriteLine($"\nBaseType:\t{baseType.Name}");
                // 接口集合
                var interfaceList = ((TypeInfo)typeTestInherit).ImplementedInterfaces;
                _testOutputHelper.WriteLine($"ImplementedInterfaces:\t{string.Join("\t", interfaceList.Select(x => x.Name))}");
                // 获取方法
                var itemList = new List<string>();
                foreach (var item in typeTestInherit.GetMethods())
                {
                    var param = item.GetParameters().Select((x, i) => $"{x.ParameterType} {x.Name},签名位置: {x.Position},是输出参数: {x.IsOut}");
                    itemList.Add($"{item.Name}:  {string.Join("  |  ", param)}");
                }
                _testOutputHelper.WriteLine($"\nMethods Name:\n\t{string.Join("\n\t", itemList)}");
                itemList.Clear();
                // 获取构造函数
                foreach (var item in typeTestInherit.GetConstructors())
                {
                    var param = item.GetParameters().Select((x, i) => $"{x.ParameterType} {x.Name},签名位置: {x.Position},是输出参数: {x.IsOut}");
                    itemList.Add($"{item.Name}:  {string.Join("  |  ", param)}");
                }
                _testOutputHelper.WriteLine($"Constructor Name:\n\t{string.Join("\n\t", itemList)}");
                itemList.Clear();
                // 获取属性
                foreach (var item in typeTestInherit.GetProperties())
                {
                    itemList.Add($"{item.PropertyType}: {item.Name}");
                }
                _testOutputHelper.WriteLine($"Propertie Name:\n\t{string.Join("\t", itemList)}");
            }

5.完整代码

        [Fact]
        public void TestInherit()
        {
            // 测试类继承
            var typeTestInherit = typeof(TestInherit);
            var typeTestInheritBase = typeof(TestInheritBase);
            var typeITestInheritBase = typeof(ITestInheritBase);
            // 测试抽象类的继承
            var typeTestAbstractInherit = typeof(TestAbstractInherit);
            var typeTestAbstractInheritBase = typeof(TestAbstractInheritBase);
            {
                var newTestInherit = new TestInherit();
                var newTestInheritBase = new TestInheritBase();

                _testOutputHelper.WriteLine($"TestInherit 继承 TestInheritBase: {newTestInherit is TestInheritBase}");// true
                _testOutputHelper.WriteLine($"TestInherit 继承 ITestInheritBase: {newTestInherit is ITestInheritBase}");// true
                _testOutputHelper.WriteLine($"TestInheritBase 继承 TestInheritBase: {newTestInheritBase is TestInheritBase}");// true
                _testOutputHelper.WriteLine($"TestInheritBase 继承 ITestInheritBase: {newTestInheritBase is ITestInheritBase}");// false
            }
            {
                _testOutputHelper.WriteLine($"\nTestInherit 继承 TestInheritBase: {typeTestInherit.IsSubclassOf(typeTestInheritBase)}");// true
                _testOutputHelper.WriteLine($"TestInherit 继承 ITestInheritBase: {typeTestInherit.IsSubclassOf(typeITestInheritBase)}");// false
                _testOutputHelper.WriteLine($"TestAbstractInherit 继承 TestAbstractInheritBase: {typeTestAbstractInherit.IsSubclassOf(typeTestAbstractInheritBase)}");// true

                _testOutputHelper.WriteLine($"\nTestInherit 继承 TestInheritBase: {typeTestInheritBase.IsAssignableFrom(typeTestInherit)}");// true
                _testOutputHelper.WriteLine($"TestInherit 继承 ITestInheritBase: {typeITestInheritBase.IsAssignableFrom(typeTestInherit)}");// true
            }
            {
                // 父类
                var baseType = typeTestInherit.BaseType;
                _testOutputHelper.WriteLine($"\nBaseType:\t{baseType.Name}");
                // 接口
                var interfaceList = ((TypeInfo)typeTestInherit).ImplementedInterfaces;
                _testOutputHelper.WriteLine($"ImplementedInterfaces:\t{string.Join("\t", interfaceList.Select(x => x.Name))}");
                // 获取方法
                var itemList = new List<string>();
                foreach (var item in typeTestInherit.GetMethods())
                {
                    var param = item.GetParameters().Select((x, i) => $"{x.ParameterType} {x.Name},签名位置: {x.Position},是输出参数: {x.IsOut}");
                    itemList.Add($"{item.Name}:  {string.Join("  |  ", param)}");
                }
                _testOutputHelper.WriteLine($"\nMethods Name:\n\t{string.Join("\n\t", itemList)}");
                itemList.Clear();
                // 获取构造函数
                foreach (var item in typeTestInherit.GetConstructors())
                {
                    var param = item.GetParameters().Select((x, i) => $"{x.ParameterType} {x.Name},签名位置: {x.Position},是输出参数: {x.IsOut}");
                    itemList.Add($"{item.Name}:  {string.Join("  |  ", param)}");
                }
                _testOutputHelper.WriteLine($"Constructor Name:\n\t{string.Join("\n\t", itemList)}");
                itemList.Clear();
                // 获取属性
                foreach (var item in typeTestInherit.GetProperties())
                {
                    itemList.Add($"{item.PropertyType}: {item.Name}");
                }
                _testOutputHelper.WriteLine($"Propertie Name:\n\t{string.Join("\t", itemList)}");
            }
        }
View Code

6.输出日志

TestInherit 继承 TestInheritBase: True
TestInherit 继承 ITestInheritBase: True
TestInheritBase 继承 TestInheritBase: True
TestInheritBase 继承 ITestInheritBase: False

TestInherit 继承 TestInheritBase: True
TestInherit 继承 ITestInheritBase: False
TestAbstractInherit 继承 TestAbstractInheritBase: True

TestInherit 继承 TestInheritBase: True
TestInherit 继承 ITestInheritBase: True

BaseType:    TestInheritBase
ImplementedInterfaces:    ITestInheritBase

Methods Name:
    get_Num:  
    set_Num:  System.Int32 value,签名位置: 0,是输出参数: False
    get_Str:  
    set_Str:  System.String value,签名位置: 0,是输出参数: False
    Run:  
    Run:  System.Int32 num,签名位置: 0,是输出参数: False
    Run:  System.Int32 num,签名位置: 0,是输出参数: False  |  System.String str,签名位置: 1,是输出参数: False
    GetType:  
    ToString:  
    Equals:  System.Object obj,签名位置: 0,是输出参数: False
    GetHashCode:  
Constructor Name:
    .ctor:  
    .ctor:  System.Int32 num,签名位置: 0,是输出参数: False
    .ctor:  System.Int32 num,签名位置: 0,是输出参数: False  |  System.String str,签名位置: 1,是输出参数: False
Propertie Name:
    System.Int32: Num    System.String: Str
View Code

 

这篇关于C# 判断一个类是否继承指定类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!