在反射中调用非静态方法

发布于 2024-12-21 02:17:32 字数 596 浏览 2 评论 0原文

我似乎无法弄清楚如何从反射调用非静态方法(实例方法)。我做错了什么?真的很新/对反射一无所知(如果你没有注意到):

示例:

class Program
{
    static void Main()
    {
        Type t = Type.GetType("Reflection.Order" + "1");
        var instance = Activator.CreateInstance(t);
        object[] paramsArray = new object[] { "Hello" };
        MethodInfo method = t.GetMethod("Handle", BindingFlags.InvokeMethod | BindingFlags.Public);

        method.Invoke(instance, paramsArray);

        Console.Read();
    }
}



public class Order1
{
    public void Handle()
    {
        Console.WriteLine("Order 1 ");
    }
}

I can't seem to figure out how to call a Non-Static method (Instance Method) From reflection. What am I doing wrong? Really new / ignorant with reflection (If you haven't noticed):

Example:

class Program
{
    static void Main()
    {
        Type t = Type.GetType("Reflection.Order" + "1");
        var instance = Activator.CreateInstance(t);
        object[] paramsArray = new object[] { "Hello" };
        MethodInfo method = t.GetMethod("Handle", BindingFlags.InvokeMethod | BindingFlags.Public);

        method.Invoke(instance, paramsArray);

        Console.Read();
    }
}



public class Order1
{
    public void Handle()
    {
        Console.WriteLine("Order 1 ");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静谧幽蓝 2024-12-28 02:17:32

您有两个问题:

  1. 您的 BindingFlags 不正确。应该是:

    MethodInfo method = t.GetMethod("Handle", BindingFlags.Instance | BindingFlags.Public);
    

    或者您可以一起删除绑定标志并使用默认绑定行为,这在这种情况下将起作用。

  2. 您声明的 Handle 方法采用零个参数,但您使用一个参数调用它 ("Hello")。要么向 Handle 添加一个字符串参数:

    public void Handle(字符串某物)
    {
        Console.WriteLine("订单 1 ");
    }
    

    或者不要传入任何参数。

You have two problems:

  1. Your BindingFlags are incorrect. It should be:

    MethodInfo method = t.GetMethod("Handle", BindingFlags.Instance | BindingFlags.Public);
    

    Or you can remove the binding flags all together and use the Default Binding behavior, which will work in this case.

  2. Your Handle method as declared takes zero parameters, but you are invoking it with one parameter ("Hello"). Either add a string parameter to Handle:

    public void Handle(string something)
    {
        Console.WriteLine("Order 1 ");
    }
    

    Or don't pass in any parameters.

ゞ记忆︶ㄣ 2024-12-28 02:17:32

您应该

BindingFlags.Instance | BindingFlags.Public

在调用 GetMethod() 时使用。

BindingFlags.InvokeMethod(和其他调用标志)不被 GetMethod() 使用。您可以在 Type.InvokeMember() 的文档中了解其含义。

You should use

BindingFlags.Instance | BindingFlags.Public

in your call to GetMethod().

BindingFlags.InvokeMethod (and other invocation flags) is not used by GetMethod(). You can see what it's meant for in the documentation for Type.InvokeMember().

清泪尽 2024-12-28 02:17:32

您需要包含 BindingFlags.Instance

You need to include BindingFlags.Instance.

紫轩蝶泪 2024-12-28 02:17:32

除了已经提到的绑定标志之外,您似乎还试图将参数传递给不接受任何参数的方法。

In addition to the binding flags already mentioned, you appear to be trying to pass an argument to a method that doesn't take any.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文