Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何列出在运行时使用MakeGenericType创建的程序集或命名空间中的类型?_C#_.net_Reflection - Fatal编程技术网

C# 如何列出在运行时使用MakeGenericType创建的程序集或命名空间中的类型?

C# 如何列出在运行时使用MakeGenericType创建的程序集或命名空间中的类型?,c#,.net,reflection,C#,.net,Reflection,考虑以下两种类型: public abstract class Abase { } public class MyGeneric<T> : Abase { } 公共抽象类数据库{ } 公共类MyGeneric:Abase{ } 如果在运行时使用MakeGenericType创建类型: 以后如何查询该类型的程序集或命名空间 var paramType = typeof (string); var myMiscType = typeof (MyGeneric<>).Ma

考虑以下两种类型:

public abstract class Abase {
}

public class MyGeneric<T> : Abase {
}
公共抽象类数据库{
}
公共类MyGeneric:Abase{
}
如果在运行时使用MakeGenericType创建类型:
以后如何查询该类型的程序集或命名空间

var paramType = typeof (string);
var myMiscType = typeof (MyGeneric<>).MakeGenericType(paramType);

var assembly = myMiscType.Assembly;
//my type is not in here:
var allTypesfromMyTypeAssembly = (
        from t in assembly.GetTypes()where typeof (Abase).IsAssignableFrom(t)select t);
var paramType=typeof(字符串);
var myMiscType=typeof(MyGeneric).MakeGenericType(paramType);
var assembly=myMiscType.assembly;
//我的类型不在这里:
变量allTypesfromMyTypeAssembly=(
从assembly.GetTypes()中的t开始,其中typeof(Abase).IsAssignableFrom(t)选择t);


我尝试了一些方法,但似乎都不管用。我想要的是可能的吗?

虽然您创建的开放泛型类型和封闭泛型类型的程序集是同一个程序集,但封闭类型不在从
assembly.GetTypes()返回的列表中,因为此列表表示编译时已知的内容


您最好将创建的类型存储在另一个列表中,并将其与
Assembly.GetTypes()

连接起来,我认为
Assembly.GetTypes()
不显示运行时配置的类型。有趣的是,在我的用例中,用户可以对“模式”进行一些控制因此,我需要为所有创建的类型构建某种类型的注册表。希望有一些我不知道的内置方法可以在AppDomain中查询任何给定时间存在的类型。