C#判断类是否继承某个类或接口

C#判断某个类是否派生某个类或是否实现了某个接口

is和as

is关键字可以确定对象实例或表达式结果是否可转换为指定类型。基本语法:

1
expr is type

如果满足以下条件,则 is 语句为 true:

  • expr 是与 type 具有相同类型的一个实例。
  • expr 是派生自 type 的类型的一个实例。 换言之,expr 结果可以向上转换为 type 的一个实例。
  • expr 具有属于 type 的一个基类的编译时类型,expr 还具有属于 type 或派生自 type 的运行时类型。 变量的编译时类型是其声明中定义的变量类型。 变量的运行时类型是分配给该变量的实例类型。
  • expr 是实现 type 接口的类型的一个实例。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
     
    public class Class1 : IFormatProvider
    {
       public object GetFormat(Type t)
       {
          if (t.Equals(this.GetType()))     
             return this;
          return null;
       }
    }
     
    public class Class2 : Class1
    {
       public int Value { get; set; }
    }
     
    public class Example
    {
       public static void Main()
       {
          var cl1 = new Class1();
          Console.WriteLine(cl1 is IFormatProvider); //True
          Console.WriteLine(cl1 is Object); //True
          Console.WriteLine(cl1 is Class1); //True
          Console.WriteLine(cl1 is Class2); //True
          Console.WriteLine();
      
          var cl2 = new Class2();
          Console.WriteLine(cl2 is IFormatProvider); //True
          Console.WriteLine(cl2 is Class2); //True
          Console.WriteLine(cl2 is Class1); //True
          Console.WriteLine();
           
          Class1 cl = cl2;
          Console.WriteLine(cl is Class1); //True
          Console.WriteLine(cl is Class2); //True
       }
    }

as运算符类似于转换运算。如果无法进行转换,则 as 会返回 null,而不是引发异常。基本语法:

1
expr as type

等效

1
expr is type ? (type)expr : (type)null

可以尝试转换,根据转换的成功与否判断类的派生关系。
参考至:

 

Type.IsSubclassOf 和 Type.IsAssignableFrom

Type.IsSubclassOf 确定当前 Type 是否派生自指定的 Type。

1
2
3
4
[ComVisibleAttribute(true)]
public virtual bool IsSubclassOf(Type c)

如果当前 Type 派生于 c,则为 True;否则为 false。 如果 当前Type 和 c 相等,此方法也返回 True。
但是IsSubclassOf方法不能用于确定接口是否派生自另一个接口,或是否类实现的接口。

Type.IsAssignableFrom 确定指定类型的实例是否可以分配给当前类型的实例。

1
2
3
public virtual bool IsAssignableFrom(Type c)

如果满足下列任一条件,则为 true:

  • c 且当前实例表示相同类型。
  • c 是从当前实例直接或间接派生的。 c 它继承自的当前实例; 如果直接从当前实例派生 c 如果它继承自一个或多个从继承类的当前实例的一系列的当前实例中间接派生。
  • 当前实例是一个 c 实现的接口。
  • c 是一个泛型类型参数,并且当前实例表示 c 的约束之一。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
 
public interface IInterface
{
   void Display();
}
 
public class Class1 { }
 
public class Implementation :Class1, IInterface
{
   public void Display()
   {
      Console.WriteLine("The implementation...");
   }
}
 
public class Example
{
   public static void Main()
   {
      Console.WriteLine("Implementation is a subclass of IInterface:   {0}",
                        typeof(Implementation).IsSubclassOf(typeof(IInterface))); //False
      Console.WriteLine("Implementation subclass of Class1: {0}",
                        typeof(Implementation).IsSubclassOf(typeof(Class1))); //True
      Console.WriteLine("IInterface is assignable from Implementation: {0}",
                        typeof(IInterface).IsAssignableFrom(typeof(Implementation))); //True
      Console.WriteLine("Class1 is assignable from Implementation: {0}",
                        typeof(Class1).IsAssignableFrom(typeof(Implementation))); //True
   }
}

可以使用 Type.IsSubclassOf 判断类的派生, 使用 Type.IsAssignableFrom 判断类的派生和接口继承。
参考至:

 

出处:https://www.cnblogs.com/snaildev/archive/2017/10/13/7661993.html

=======================================================================================

C# 两个类是否继承关系

IsAssignableFrom:确定指定类型的实例是否可以分配给当前类型的实例

B继承自A

static void Main(string[] args)
        {
            Type a = typeof(A);
            Type b = typeof(B);
            Console.WriteLine(a.IsAssignableFrom(b));       //true
            Console.WriteLine(b.IsAssignableFrom(a));       //false
 
            Type c = typeof(Nullable<int>);
            Type d = typeof(int);
            Console.WriteLine(c.IsAssignableFrom(d));       //true
            Console.WriteLine(d.IsAssignableFrom(c));       //false
 
            Console.ReadLine();
        }
 
        class A { }
        class B : A { }

 

出处:https://www.cnblogs.com/hanjun0612/p/9779722.html

=======================================================================================

c#中判断类是否继承于泛型基类

 更新时间:2022年04月14日 08:11:15   作者:j4587698  
 
这篇文章主要介绍了c#中判断类是否继承于泛型基类,最简单的办法肯定是typeof(b).baseType=typeof(a<string>)这样就可以返回true,但是这种方法时不妥的,下面看看如何解决这个问题呢

在c#中,有时候我们会编写类似这样的代码:

1
2
3
4
public class a<T>
{
    //具体类的实现
}
public class b : a<string>{}
 

如果b继承a的类型不确定,这个时候我们是无法通过baseType来直接判断b是否继承于a的。

如果我们写如下代码:

1
typeof(b).baseType == typeof(a)
 

返回值是false

因为typeof(b).baseType返回的类型是a`1[System.String],而typeof(a<>)返回的是a`1[T]。很明显这两个类型是不相等的。所以上面返回flase也就是正常现象了。

那么如何解决这个问题呢?

最简单的办法肯定是typeof(b).baseType == typeof(a<string>)这样就可以返回true。

但是由于我们用了T,所以大概率我们是不知道进来的类型的,所以这种方法不行。

另一种方法麻烦一点。

首先,我们把泛型类型转换成泛型原型,然后再去比较泛型原型就可以了。

c#中提供了获取泛型原型的方法GetGenericTypeDefinition(),MSDN解释如下:

返回一个表示可用于构造当前泛型类型的泛型类型定义的 Type 对象。

所以我们直接使用typeof(b).baseType.GetGenericTypeDefinition()既可获取a<>

这里需要注意的是GetGenericTypeDefinition()在b不是泛型时会抛出异常,所以我们在使用前应该判断b是否是一个泛型类。

c#中同样提供了这个方法IsGenericType,MSDN解释如下:

获取一个值,该值指示当前类型是否是泛型类型。

所以我们判断一个类是否继承于泛型基类的方法就是这样的:

1
2
3
if (typeof(b).baseType.IsGenericType && typeof(b).baseType.GetGenericTypeDefinition() == typeof(a))
{
// 这里是b继承于a<T>的逻辑
}
 

到此这篇关于c#中判断类是否继承于泛型基类的文章就介绍到这了,更多相关c#判断类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

 

出处:https://www.jb51.net/article/244410.htm

posted on 2022-10-21 10:06  jack_Meng  阅读(3367)  评论(0编辑  收藏  举报

导航