J. *_*Doe 5 .net c# reflection inheritance
假设我有一个父类和一个子类:
public abstract class Parent
{
public string GetParentName()
{
return GetType().Name;
}
}
public class Child : Parent
{
public string GetChildName()
{
return GetType().Name;
}
}
Run Code Online (Sandbox Code Playgroud)
截至目前,GetParentName()和GetChildName()都将返回Child.
但是,在我的场景中,我想获取声明方法的类的名称.
因此GetChildName()应返回Child但GetParentName()应返回Parent.
这有可能吗?
注意:
我知道我可以使用GetType().BaseType.Name但这不起作用,因为层次结构可能很复杂.
你想要这个,我想:
return MethodBase.GetCurrentMethod().DeclaringType.Name;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
84 次 |
| 最近记录: |