将函数名称作为参数传递给函数 [英] Passing a function name as parameter to function

查看:201
本文介绍了将函数名称作为参数传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys,

我很难将函数名称作为参数传递给另一个函数。

我有以下设置:

1.有2个类(A和B)

2.我正在尝试在A类中编写一个函数来执行B类中的方法。



我知道我可以传递一个类实例,然后从该实例化类中运行该方法...但我也能找到一种方法来传递方法名称(不要手动输入名称。

我在这里找到了一个很好的例子:

http://stackoverflow.com/questions/2082615/pass-method-as-parameter-using-c-sharp [ ^ ]

但问题是这个例子展示了如何传递同一个类的方法名称(换句话说y)你可以直接访问该方法,而无需实例化类)....在我的情况下,这是一个有点不同的故事 - 我必须实例化该类....



先谢谢你了!



Modestas

Hello Guys,
I am having a hard time with passing a function name as a parameter to another function.
I have the following setup:
1. There are 2 classes (A and B)
2. I am trying to write a function in class A which would be executing methods from class B.

I know I can pass a class instance and then run the method from that instantiated class... but I just can find a way to also pass a method name as well (don't want to type the name manually).
I found a good example over here:
http://stackoverflow.com/questions/2082615/pass-method-as-parameter-using-c-sharp[^]
But the problem is that this example shows how to pass method name of the same class (in other words you can access the method directly without having to instantiate the class).... in my case it's a bit different story - I have to instantiate the class....

Thank you in advance guys!

Modestas

推荐答案

大家好,你好汤姆,

我解决了这个问题,你给我的链接非常有用(MSDN中的代表)



解决方法就是我通过了委托给函数而不是传递函数名。在此之前,我将函数分配给这样的委托:



Hello Everyone, hello Tom,
I got this solved and the link you gave me was very uselfull (the for delegates in MSDN)

The solution for this was that I passed delegate to the function instead of passing the function name. And before that I assign the function to delegate like this:

private delegate void my_delegate(string name); //create delegate that takes string as an argument

Class_A class_instance = new Class_A(); //create class instance
my_delegate = class_instance.Function_I_want_to_Pass; //assign the delegate

//now I can use this:
private void Function_Caller(argument_1, argument_2, my_delegate)
{
    //do something with argument #1 and argument #2
    //.......
    //call the function (delegated)
    my_delegate("Modestas");
}
//closing the class instance
class_instance.Close();





现在我可以将委托传递给函数(所以就像传递函数名一样)。

当然 - 这样我在代码中创建了更多的行,但这并不麻烦,因为我仍然可以达到通用函数调用者的目标。



希望这对任何人都有帮助。

非常感谢!



Modestas。



now I can pass the delegate to the function (so it's just like passing the function name).
Of course - this way I create few more lines in the code but this does not bother as I can still achieve the goal of universal function caller.

Hope this helps anyone.
Thanks a lot!

Modestas.






我不知道这是否有用,但你可以使用反射来执行一个类的方法。



http://stackoverflow.com/questions/2202381/reflection-how -to-invoke-with-parameters-parameters [ ^ ]



因此,如果你实例化一个b类实例,那么使用反射来查找哪一个方法名称已经传递到A类的方法中,它可能达到你想要的效果。



虽然它的性能可能很大。
Hi,

I do not know if this will help, but you could use reflection to execute a method off of a class.

http://stackoverflow.com/questions/2202381/reflection-how-to-invoke-method-with-parameters[^]

So if you instantiate an instance of class b then use reflection to find which ever method name has been passed into the method on class A it might achieve what you want.

Although it's performance hit might be substantial.


在ClassB中

In ClassB
public static void doStuffInB()
       {
       //do some really cool stuff.
       }




A类




in ClassA:

private void Form1_Load(object sender, EventArgs e)
        {

            ClassB.doStuffInB();
        }


这篇关于将函数名称作为参数传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆