Get derived type from static method
c#, reflection, static, types
Solution
Assuming you mean you have something like this
class MyBaseClass
{
public static void DoSomething()
{
Console.WriteLine(/* current class name */);
}
}
class MyDerivedClass : MyBaseClass
{
}
and want `MyDerivedClass.DoSomething();` to print `"MyDerivedClass"`, then the answer is:
There is no solution to your problem. Static methods are not inherited like instance methods. You can refer to `DoSomething` using `MyBaseClass.DoSomething` or `MyDerivedClass.DoSomething`, but both are compiled as calls to `MyBaseClass.DoSomething`. It is not possible to find out which was used in the source code to make the call.
Problem
I want to get derived type from static method. I want to do something like this ``` void foo() { this.getType(); } ``` but in static method I know that ``` MethodBase.GetCurrentMethod().DeclaringType ``` returns base type, but i need derived.