BenjaminApprill
BenjaminApprill

Reputation: 63

Query for static type in Linq

When I query for abstract types using Linq, it also grabs static classes.

IEnumerable<Type> FilterInheritable()
{
     var q = Assembly.Load("Assembly-CSharp").GetTypes()
       .Where(x => x.IsAbstract == true);            

     return q;
}

Is it possible to filter out the static types? Something like this?

IEnumerable<Type> FilterInheritable()
{
     var q = Assembly.Load("Assembly-CSharp").GetTypes()
       .Where(x => x.IsAbstract == true)
       .Where(x => x.IsStatic != true);

     return q;
}

Upvotes: 3

Views: 270

Answers (1)

Ren&#233; Vogt
Ren&#233; Vogt

Reputation: 43906

Since static classes are also sealed by definition, but abstract classes cannot be sealed, you can do this:

var q = Assembly.Load("Assembly-CSharp").GetTypes()
                .Where(x => x.IsAbstract && x.IsClass && !x.IsSealed);

I added IsClass to exclude interfaces as well.

Upvotes: 4

Related Questions