希望标题不太混乱,但是可以了。
说我有一个通用接口
public interface IBase<T>{
}
后接实现多个接口(但具有不同的泛型)的类,如下所示:
public class Base : IBase<TypeA>, IBase<TypeB>{
}
我将如何检索实现该接口的类的列表?我当前的解决方案:
from x in AppDomain.currentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
from z in x.GetInterfaces()
where
z.IsGenericType &&
basetype.IsassignableFrom(z.GetGenericTypeDefinition())
select x
问题是它检索多个相同的类。
任何帮助将不胜感激!
我会选择Distinct <>()。
(from x in AppDomain.currentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
from z in x.GetInterfaces()
where
z.IsGenericType &&
basetype.IsassignableFrom(z.GetGenericTypeDefinition())
select x)
.Distinct();