Ronnie Overby
Ronnie Overby

Reputation: 46470

C# - How to get certain generic types from an assembly using linq

I'm trying to find types in an assembly that are of ISomeInterface< AnyType > using linq.

How do I do this?

Here's what I have:

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

Upvotes: 1

Views: 518

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062905

Something like (I'm not at a pc)

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;

Upvotes: 2

Related Questions