Getting the name of the declaring class?

Suppose I have a parent class and a child class:

public abstract class Parent { public string GetParentName() { return GetType().Name; } } public class Child : Parent { public string GetChildName() { return GetType().Name; } } 

At current, both GetParentName () and GetChildName () will return Child.

However, in my scenario, I would like to get the name of the class in which the method is declared.

Thus, GetChildName () should return a Child, but GetParentName () should return a parent.

Is it possible?

Note:

I understand that I can use GetType (). BaseType.Name, but this will not work, as the hierarchy can be complex.

+5
source share
1 answer

You want this, I think:

 return MethodBase.GetCurrentMethod().DeclaringType.Name; 
+6
source

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


All Articles