我有 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?
最佳答案
var methodInfo = this.GetType().GetMethod("AddText", BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(string) }, null);
您的方法有一个参数,您需要使用接受参数类型和绑定(bind)标志的类型数组的重载。
在 .net 中,方法签名基于它们的名称、返回类型和参数。
因此,如果您的方法有参数,您必须通过 Type[] 告诉 Reflection 它有哪些参数类型。
关于C# - GetMethod 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31388939/