假设我有一个父类和一个子类:
public abstract class Parent
{
public string GetParentName()
{
return GetType().Name;
}
}
public class Child : Parent
{
public string GetChildName()
{
return GetType().Name;
}
}
截至目前,GetParentName() 和 GetChildName() 都将返回 Child。
但是,在我的场景中,我想获取声明该方法的类的名称。
因此 GetChildName() 应该返回 Child 但 GetParentName() 应该返回 Parent。
这有可能吗?
注意:
I understand that I could use GetType().BaseType.Name but this won't work since the hierarchy could be complex.
最佳答案
我想你想要这个:
return MethodBase.GetCurrentMethod().DeclaringType.Name;
关于c# - 获取声明类的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41247534/