Gre*_*ane 10 c# generics reflection class
我正在尝试列出由泛型类创建的所有运行时构造类.换句话说,如果我有一个班级:
public GenericCls<T> {
public void Reset() { ... }
...
}
Run Code Online (Sandbox Code Playgroud)
我在这样的各个地方都有代码:
GenericCls<int> gci = new GenericCls<int>();
GenericCls<String> gcs = new GenericCls<String>();
GenericCls<float> gcf = new GenericCls<float>();
...
Run Code Online (Sandbox Code Playgroud)
我可以得到这样的东西吗?:
Type[] allconstructed = GetAllConstructed(typeof(GenericCls<>));
Run Code Online (Sandbox Code Playgroud)
返回 {GenericCls<int>,GenericCls<String>,GenericCls<float>,...}
用例涉及一个通用的分配器,它支持任何类型的对象分配(就像new XXX()垃圾收集器一样好,但更好).我不会详细说明,因为它只会使问题复杂化.基本上,我不会在编译时知道所有构造的类,因为该库是一个旨在与单独的代码项目一起使用的DLL.所以我需要某种形式的反射,我似乎无法在互联网上找到.
Assembly.GetExecutingAssembly().GetExportedTypes()除了基本泛型类(即typeof(GenericCls<>))之外不包含任何内容
typeof(GenericCls<>).GetGenericArguments() 仅返回类型"T",它不仅是无效类型,而且完全没用.
如果只知道泛型类的类型,是否甚至可以找到泛型类的所有构造类?(typeof(GenericCls<>);)我不确定"构造"是否是正确的词 - 我想知道当前活动的所有具体泛型派生类,或者所有这些将存在的(不确定C#如何处理幕后的通用构造) ).
| 归档时间: |
|
| 查看次数: |
766 次 |
| 最近记录: |