检索实现多个相同泛型接口的类的唯一列表

问题描述 投票:0回答:1

希望标题不太混乱,但是可以了。

说我有一个通用接口

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

问题是它检索多个相同的类。

任何帮助将不胜感激!

c# reflection
1个回答
0
投票

我会选择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();
© www.soinside.com 2019 - 2024. All rights reserved.