C# - GetMethod 返回 null

标签 c#

我有 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/

相关文章:

c# - .net Framework 4.0 中地理 sql server 数据类型的 C# 等价物是什么?

c# - 面向营销人员的 Sitecore 网络表单保存项目操作

c# - C# 可以并将对象写入页面文件吗?

c# - 如何在 Visual Studio 中使用 XUnit 对在远程进程中运行的代码进行单元测试

c# - 我正在尝试将对象转换为动态类型,但转换失败并出现 RunTimeBinder 异常

c# - Outlook 2010 中的命令栏

c# - 为什么不能捕获内部异常?

c# - 使用Windows.Media.Transcoding从WAV创建m4a

c# - 这些按位运算在做什么

c# - CTRL+R CTRL+T 不适用于 VisualStuidio2017