通用类型过滤 [英] Generic type filtering
本文介绍了通用类型过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
假设我们有一个抽象类X< t1,t2>并且w有两个派生类Y:X< int,float>和Z:X< float,string> ;.
现在,我想获得实现抽象类X的所有类型。如何在typeof中定义类型()做到这一点?
Hello,
Lets assume that we have an abstract class X<t1,t2> and w have two derived classes Y:X<int,float> and Z:X<float,string>.
Now, I wanna to get all types that implements the abstract class X. How can I define the type in the typeof() to do that?
推荐答案
你需要使用反射。
以下课程: code> System.Reflection.Assembly
具有以下方法:GetTypes
这将返回该程序集中定义的所有类型的数组。然后,对于每种类型,您需要以递归方式检查抽象类X
的BaseType
属性。
You need to use reflection.
The following class:System.Reflection.Assembly
Has the following method:GetTypes
This returns an array of all the types defined within that assembly. Then for each type you need to recursively check theBaseType
property for abstract classX
.
这篇关于通用类型过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文