C# 使用反射从基础类型获取方法名称
我想列出底层类型的所有方法名称。
我尝试过
var methods = this.GetType().UnderlyingSystemType.GetMethods();
但没有成功。
编辑
添加了我需要的示例。
public class BaseClass
{
public BaseClass()
{
var methods = this.GetType().UnderlyingSystemType.GetMethods();
}
}
public class Class1:BaseClass
{
public void Method1()
{}
public void Method2()
{}
}
在集合 Method1 和 Method 2 中
I'd like to list all method names from the underlyingtype.
I tried
var methods = this.GetType().UnderlyingSystemType.GetMethods();
but doesn't work.
EDIT
Added example
public class BaseClass
{
public BaseClass()
{
var methods = this.GetType().UnderlyingSystemType.GetMethods();
}
}
public class Class1:BaseClass
{
public void Method1()
{}
public void Method2()
{}
}
I need in the collection Method1 and Method 2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您提供的代码有效。
返回
编辑:
这是你想要的吗?
The code you provided works.
returns
EDIT:
Is that what you want?
尝试类似的东西
Try something like
问题在于您在 BaseClass 的构造函数中调用的 GetType 的重写。
如果您创建 Class1 类型的实例,并查看您拥有的方法,您将看到所有 6 个方法。
如果您创建 BaseClass 类型的实例,您将只会看到 4 个方法 - 来自 Object 类型的 4 个方法。
通过创建子类的实例,您隐式调用了 BaseClass 中的构造函数。当它使用 GetType() 时,它使用 Class1 类型的重写虚拟方法,该方法返回预期的响应。
The problem lies in the override of GetType which you are calling in the constructor of the BaseClass.
If you create an instance of the Class1 type, and look at the methods you have, you will see all 6 methods.
If you create an instance of the BaseClass type, you will only see 4 methods - the 4 methods on from the Object type.
By creating an instance of the subclass, you are implicitly calling the constructor in the BaseClass. When it uses GetType(), it uses the overridden virtual method of the Class1 type, which returns the expected response.