如何将方法名称传递给另一个方法并通过委托变量调用它?

发布于 2024-11-08 04:04:30 字数 816 浏览 0 评论 0原文

我有一个方法,其中包含指向另一个类的委托变量。 我想通过此委托调用该类中的方法,但将该方法的名称作为字符串传递给包含该委托的方法。

这怎么能做到呢?使用反射? 功能

编辑:

我现在明白反射可能不是最好的解决方案。

这就是我所拥有的:

private static void MethodContainingDelegate(string methodNameInOtherClassAsString)
{
        _listOfSub.ForEach(delegate(Callback callback)
        {
            //Here the first works, but I want the method to be general and   
            //  therefore pass the method name as a string, not specfify it. 
            callback.MethodNameInOtherClass(); 
            //This below is what I am trying to get to work. 
             callback.MethodNameInOtherClassAsString();                  
          }
     });
}

所以,基本上,我正在寻找一种方法让我的回调委托“识别”我的 methodNameInOtherClassAsString 实际上是在另一个类中执行的方法。

谢谢!

I have a method that contains a delegate variable that points at another class.
I want to call a method in that class via this delegate, but pass the name of the method as a string to the method containing the delegate.

How can this be done? Using reflection? Func<T>?

Edit:

I understand now that reflection may not be the best solution.

This is what I have:

private static void MethodContainingDelegate(string methodNameInOtherClassAsString)
{
        _listOfSub.ForEach(delegate(Callback callback)
        {
            //Here the first works, but I want the method to be general and   
            //  therefore pass the method name as a string, not specfify it. 
            callback.MethodNameInOtherClass(); 
            //This below is what I am trying to get to work. 
             callback.MethodNameInOtherClassAsString();                  
          }
     });
}

So, basically, I am looking for a way to make my callback delegate "recognize" that my methodNameInOtherClassAsString is actually a method to execute in the other class.

Thanks!

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

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

发布评论

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

评论(3

诗笺 2024-11-15 04:04:30

很简单:

public delegate void DelegateTypeWithParam(object param);
public delegate void DelegateTypeWithoutParam();

public void MethodWithCallbackParam(DelegateTypeWithParam callback, DelegateTypeWithoutParam callback2)
{
    callback(new object());
    Console.WriteLine(callback.Method.Name);
    callback2();
    Console.WriteLine(callback2.Method.Name);
}

// must conform to the delegate spec
public void MethodWithParam(object param) { }
public void MethodWithoutParam() { }

public void PassCallback()
{
   MethodWithCallbackParam(MethodWithParam, MethodWithoutParam);
}

委托变量指向什么类并不重要。它可以在另一个类中定义——没有太大区别。

我认为您甚至可以从委托变量本身查询原始方法的名称,而无需反射。每个委托都有一个名为 Method 的属性,专门用于此目的。

It's very simple:

public delegate void DelegateTypeWithParam(object param);
public delegate void DelegateTypeWithoutParam();

public void MethodWithCallbackParam(DelegateTypeWithParam callback, DelegateTypeWithoutParam callback2)
{
    callback(new object());
    Console.WriteLine(callback.Method.Name);
    callback2();
    Console.WriteLine(callback2.Method.Name);
}

// must conform to the delegate spec
public void MethodWithParam(object param) { }
public void MethodWithoutParam() { }

public void PassCallback()
{
   MethodWithCallbackParam(MethodWithParam, MethodWithoutParam);
}

It doesn't matter, what class does the delegate variable point to. It can be defined in another class -- there's not much difference.

I think you could even query the name of the original method from the delegate variable itself without reflection. Every delegate has a property called Method exactly for that.

寄居人 2024-11-15 04:04:30

您可以执行以下操作:

var mi = typeof(Foo).GetMethods().Single(x => x.Name == "Bar");
mi.Invoke(foo, null);

其中 Foo 是您的目标类,Bar 是您要调用的方法。
但你应该注意到,反射会对你的程序的性能产生很大的影响。考虑改用强类型委托。

You can do something like this:

var mi = typeof(Foo).GetMethods().Single(x => x.Name == "Bar");
mi.Invoke(foo, null);

Where Foo is your target class, Bar is the method you want to call.
But you should note that reflection will have a great impact on your program's performance. Consider using strongly typed delegates instead.

裂开嘴轻声笑有多痛 2024-11-15 04:04:30

假设您有方法名称的字符串表示形式:

var methodInfo = myObject.GetType().GetMethod(myString); //<- this can throw or return null
methodInfo.Invoke(myObject, new object[n]{parm1, pram2,...paramn});

显然您需要为此添加一些错误检查,并且应该使用更具体的版本 GetMethod 如果可以的话。

Assuming you have string representation of the method name:

var methodInfo = myObject.GetType().GetMethod(myString); //<- this can throw or return null
methodInfo.Invoke(myObject, new object[n]{parm1, pram2,...paramn});

you need to add some error checking to this obviously, and should use a more specific version of GetMethod if you can.

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