在下面的示例中,输出仅为:
Foo`1[T]
我正在寻找封闭式的类型:
Foo`1[System.Int32]
有没有办法查看运行时基于我的开放泛型类型创建的已关闭类型?
class Foo<T>
{
}
class Program
{
static void Main(string[] args)
{
var tmp = new Foo<int>();
ListTypes();
}
private static void ListTypes()
{
var types = from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
where type.Name.Contains("Foo")
select type;
foreach (var type in types)
Console.WriteLine(type.ToString());
}
}
我也尝试通过泛型参数查找所有类型,希望能发现闭合类型。
class Foo<T>
{
}
class Bar
{
}
class Program
{
static void Main(string[] args)
{
var tmp = new Foo<Bar>();
ListTypes();
}
private static void ListTypes()
{
var types = from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
where type.IsGenericType
&& type.GetGenericArguments().Contains(typeof(Bar))
select type;
foreach (var type in types)
Console.WriteLine(type.ToString());
}
}
这只是为了满足我的好奇心。