How to check if a given type is a static class?

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 populate any static class from my result.

+3
source share
3 answers
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

+6
source

Everything that you do will be based on heuristics - there is no concrete "this class is static" at the IL level. And there is no guarantee that C # and VB compilers will use static / modules in future releases.

Well, a static class will not have common constructors and will be sealed, so that might be enough:

    var types=from m in System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
              where m.IsClass && (!m.IsSealed || m.GetConstructors().Any())
              select m;
+2
source

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

+1
source

Source: https://habr.com/ru/post/1768560/


All Articles