C# - 如何使用linq从程序集中获取某些泛型类型

时间:2010-09-18 17:32:10

标签: c# .net reflection

我正在尝试在ISomeInterface<组件中找到类型。 AnyType>使用linq。

我该怎么做?

这就是我所拥有的:

AppDomain.CurrentDomain.GetAssemblies().SelectMany(a=>a.GetTypes().Where(t=> /* t is ISomeInterface<ofAnyType> */))

1 个答案:

答案 0 :(得分:2)

像(我不在电脑里)

from asm in AppDomain.CurrentDomain.GetAssemblies()
from type in asm.GetTypes()
where (type.IsClass || type.IsStruct)
  && type.GetInterfaces().Any(
  intf => intf.IsGenericType
    && intf.GetGenericTypeDefinition() == typeof(ISomeInterface<>))
select type;