12 04 2013
How to check if a class is static
With the abstract class Type you can get alot of information about anything in C#. But although there are alot of Is… properties, there is no IsStatic.
If you want to know if a class is static you have to use IsAbstract and IsSealed. When both are true then you
have a static class.
To quote Microsoft:
“Do declare static classes as sealed and abstract, and add a private instance constructor, if your programming language does not have built-in support for static classes.”
Explanation:
A abstract class and a static as well can not be instantiated. This is checked with IsAbstract. But abstract alone isn’t enough.
A static class is also sealed, because it can’t be inherited unlike an abstract class, which should inherited.
1 2 3 4 |
public static bool IsStatic(Type type) { return type.IsAbstract & type.IsSealed; } |
Debug another application Creating a process monitor