Get TypeInfo in static constructor
-
06-07-2019 - |
Question
Is there any way to get the equivalent of GetType within a static constructor?
I want to iterate through the available properties of the type within the static constructor but GetType is an instance method? Why is this? The typeinfo should exist in the static context. Is there a way around this?
Solution
Just use
Type type = typeof(TheCurrentType);
It should never be more complex than this since you always know the actual type; there's no polymorphism to deal with in static methods.
OTHER TIPS
I don't think you can get derived types (other than by iterating through all types to see what derives from the current type). To get the current type, you can:
Type currentType = (new StackFrame()).GetMethod().DeclaringType;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow