4
var types=from m in System.Reflection.Assembly.Load("System").GetTypes()
                  where m.IsClass
                  where // something to check whether or not the type is a static class.
                  select m;

I want to fillter out any static class from my result.

Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165
  • This doesn't help you: http://stackoverflow.com/questions/1175888/determine-if-a-type-is-static ? – Patko Oct 08 '10 at 06:20

3 Answers3

7
var types = from m in System.Reflection.Assembly.Load("System").GetTypes()
            where m.IsClass && m.IsAbstract && m.IsSealed
            select m;

from this thread.

EDIT: Check m.IsSealed

Community
  • 1
  • 1
bla
  • 5,350
  • 3
  • 25
  • 26
2

Whatever you do is going to be based on a heuristic - there's no specific "this class is static" at the IL level. And there's no guarantee on how the C# and VB compilers will implement static/module in future releases.

Well, a static class will have no public constructors, and will be sealed, so the following may be enough:

    var types=from m in System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
              where m.IsClass && (!m.IsSealed || m.GetConstructors().Any())
              select m;
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
1

You need to check if the class is Sealed and Abstract.
The CLR does not know static classes but it does support sealed abstract classes and even though you cannot explicitly compile them, static classes are being compile to sealed abstract classes.

Itay Karo
  • 17,924
  • 4
  • 40
  • 58