当前位置: 移动技术网 > IT编程>开发语言>.net > .NET/C#如何判断某个类是否是泛型类型或泛型接口的子类型详解

.NET/C#如何判断某个类是否是泛型类型或泛型接口的子类型详解

2018年09月21日  | 移动技术网IT编程  | 我要评论

藏獒的价格,鬼吹灯外传倒斗新星卡,北京站到北京西站

前言

泛型:通过参数化类型来实现在同一份代码上操作多种数据类型。利用“参数化类型”将类型抽象化,从而实现灵活的复用。在.net类库中处处都可以看到泛型的身影,尤其是数组和集合中,泛型的存在也大大提高了程序员的开发效率。更重要的是,c#的泛型比c++的模板使用更加安全,并且通过避免装箱和拆箱操作来达到性能提升的目的。因此,我们很有必要掌握并善用这个强大的语言特性。

c#泛型特点:

1、如果实例化泛型类型的参数相同,那么jit编辑器会重复使用该类型,因此c#的动态泛型能力避免了c++静态模板可能导致的代码膨胀的问题。

2、c#泛型类型携带有丰富的元数据,因此c#的泛型类型可以应用于强大的反射技术。

3、c#的泛型采用“基类、接口、构造器,值类型/引用类型”的约束方式来实现对类型参数的“显示约束”,提高了类型安全的同时,也丧失了c++模板基于“签名”的隐式约束所具有的高灵活性

.net 中提供了很多判断某个类型或实例是某个类的子类或某个接口的实现类的方法,然而这事情一旦牵扯到泛型就没那么省心了。

本文将提供判断泛型接口实现或泛型类型子类的方法。

.net 中没有自带的方法

对于实例,.net 中提供了这些方法来判断:

if (instance is foo || instance is ifoo)
{
}

对于类型,.net 中提供了这些方法来判断:

if (typeof(foo).isassignablefrom(type) || typeof(ifoo).isassignablefrom(type))
{
}

或者,如果不用判断接口,只判断类型的话:

if (type.issubclassof(typeof(foo)))
{
}

对于 typeof 关键字,不止可以写 typeof(foo) ,还可以写 typeof(foo<>)  。这可以得到泛型版本的 foo<t> 的类型。

不过,如果你试图拿这个泛型版本的 typeof(foo<>) 执行上述所有判断,你会发现所有的 if 条件都会是 false 。

我们需要自己编写方法

typeof(foo<>)typeof(foo<someclass>) 之间的关系就是 getgenerictypedefinition 函数带来的关系。

所以我们可以充分利用这一点完成泛型类型的判断。

比如,我们要判断接口:

public static bool hasimplementedrawgeneric(this type type, type generic)
{
 // 遍历类型实现的所有接口,判断是否存在某个接口是泛型,且是参数中指定的原始泛型的实例。
 return type.getinterfaces().any(x => generic == (x.isgenerictype ? x.getgenerictypedefinition() : x));
}

而如果需要判断类型,那么就需要遍历此类的基类了:

public static bool issubclassofrawgeneric([notnull] this type type, [notnull] type generic)
{
 if (type == null) throw new argumentnullexception(nameof(type));
 if (generic == null) throw new argumentnullexception(nameof(generic));

 while (type != null && type != typeof(object))
 {
 istherawgenerictype = istherawgenerictype(type);
 if (istherawgenerictype) return true;
 type = type.basetype;
 }

 return false;

 bool istherawgenerictype(type test)
 => generic == (test.isgenerictype ? test.getgenerictypedefinition() : test);
}

于是,我们可以把这两个方法合成一个,用于实现类似 isassignablefrom 的效果,不过这回将支持原始接口(也就是 typeof(foo<>) )。

/// <summary>
/// 判断指定的类型 <paramref name="type"/> 是否是指定泛型类型的子类型,或实现了指定泛型接口。
/// </summary>
/// <param name="type">需要测试的类型。</param>
/// <param name="generic">泛型接口类型,传入 typeof(ixxx<>)</param>
/// <returns>如果是泛型接口的子类型,则返回 true,否则返回 false。</returns>
public static bool hasimplementedrawgeneric([notnull] this type type, [notnull] type generic)
{
 if (type == null) throw new argumentnullexception(nameof(type));
 if (generic == null) throw new argumentnullexception(nameof(generic));

 // 测试接口。
 var istherawgenerictype = type.getinterfaces().any(istherawgenerictype);
 if (istherawgenerictype) return true;

 // 测试类型。
 while (type != null && type != typeof(object))
 {
 istherawgenerictype = istherawgenerictype(type);
 if (istherawgenerictype) return true;
 type = type.basetype;
 }

 // 没有找到任何匹配的接口或类型。
 return false;

 // 测试某个类型是否是指定的原始接口。
 bool istherawgenerictype(type test)
 => generic == (test.isgenerictype ? test.getgenerictypedefinition() : test);
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网