【发布时间】:2017-04-22 06:19:18
【问题描述】:
我有A 类:
public abstract class A
{
}
然后我有派生自它的B 类:
public sealed class B : A
{
public void SomeMethod()
{
var method = this.GetType().GetMethod("AddText");
}
private void AddText(string text)
{
...
}
}
为什么GetMethod 返回 null?
【问题讨论】:
-
请注意,如果您只是在 Visual Studio 中将鼠标悬停在
GetMethod(...)上,它会明确指出Searches for the **public** method with the specified name.从该消息中我很明显需要另一个重载。 -
但是为什么不直接打电话给
AddText呢? -
@Grundy,可能是因为这只是一个说明真正问题的玩具问题。所以这里是否有更直接的做事方式无关紧要
-
@Graviton,可能,但你不能确定 :)
标签: c#