3

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;
}
  • 1
    Note that you don't need to compare a `bool` to `true`, the result of the comparison is `bool again. So `x => x.IsAbstract` is the same as `x => x.IsAbstract == true`. – René Vogt Dec 19 '19 at 07:15

1 Answers1

4

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.

René Vogt
  • 43,056
  • 14
  • 77
  • 99