C#传递单个方法作为参数 [英] C# passing single method as parameter
问题描述
大家好,我正在尝试这样做:
我有一个名为Jobs的课程。在该类中,我有一个名为Update的方法。我想要一个方法作为参数传递零参数。见下面的例子。
Ex:
班级工作
public 工作(){
//
}
public void 更新(wantToPassMethodHere){
// 执行wantToPassMethodHere方法
}
主类
私人工作职位= new Job();
public void 更新(){
job.Update(写信息());
}
public void WriteMessage() {
Console.WriteLine( hi);
}
这可能吗?
我尝试了什么:
我试过研究这个问题,找不到有效的解决方案。
是的,可以使用代表教程( C#) [ ^ ]
参考这个例子:
public delegate void MyMethod();
public delegate int AnotherMethodWithParams ( int a);
public class MyClass
{
public MyClass()
{
var obj = 新 Job();
obj.Update(DoSomething);
int answer = obj.Square(Square);
}
void DoSomething(){
// 您的代码
}
int Square( int a){ return a * a; }
}
public class Job
{
public void 更新(MyMethod方法) )
{
method.Invoke();
}
public int Square(AnotherMethodWithParams方法)
{
return method.Invoke( 2 );
}
}
是的,您可以通过实现您的愿望目标行动代表
关于行动代表
您可以使用此委托将方法作为参数传递,而无需显式声明自定义委托。封装的方法必须对应于此委托定义的方法签名。这意味着封装的方法必须没有参数且没有返回值。 (在C#中,该方法必须返回void。在Visual Basic中,它必须由Sub ... End Sub构造定义。它也可以是返回忽略值的方法。)通常,这样的方法用于执行一个操作。
示例: -
班级工作
public 类作业
{
public void update(动作wantToPassMethodHere)
{
wantToPassMethodHere();
}
}
主类
public class 计划
{
public static void testMethod()
{
Console.WriteLine( Hello Test Method);
}
public static void Main( string [] args)
{
// 在这里,您可以实例化您的工作类并调用更新方法
job jb = new job();
jb.update(testMethod);
// 这就是你可以测试这段代码,这是一个有效的例子。
}
}
Hello all, I am attempting to do something like this:
I have a class called "Jobs". Inside that class I have a method called "Update". I am wanting to pass a method as a parameter with zero arguments. See example below.
Ex:
Class Job
public Job(){
//
}
public void Update(wantToPassMethodHere){
//Execute wantToPassMethodHere method
}
Main Class
private Job job = new Job();
public void Update(){
job.Update(WriteMessage());
}
public void WriteMessage(){
Console.WriteLine("hi");
}
Is this possible?
What I have tried:
I have tried researching the issue, couldn't find a valid solution.
Yes It is possible, use Delegates Tutorial (C#)[^]
refer this example:
public delegate void MyMethod(); public delegate int AnotherMethodWithParams(int a); public class MyClass { public MyClass () { var obj = new Job(); obj.Update(DoSomething); int answer = obj.Square(Square); } void DoSomething() { // your code } int Square(int a) { return a * a; } } public class Job { public void Update(MyMethod method) { method.Invoke(); } public int Square(AnotherMethodWithParams method) { return method.Invoke(2); } }
Yes, It is possible, you can achieve your desire goal by usingAction Delegate
About Action Delegate
You can use this delegate to pass a method as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have no parameters and no return value. (In C#, the method must return void. In Visual Basic, it must be defined by the Sub…End Sub construct. It can also be a method that returns a value that is ignored.) Typically, such a method is used to perform an operation.
EXAMPLE:-
Class Job
public class job { public void update(Action wantToPassMethodHere) { wantToPassMethodHere(); } }
Main Class
public class Program { public static void testMethod() { Console.WriteLine("Hello Test Method"); } public static void Main(string[] args) { //Here you can instantiate your job class and called update method job jb = new job(); jb.update(testMethod); //That's it you can test this code, This is working example. } }
Note:- If you want to learn more about Action delegate, you can read here.
and here you can read parameterize Action<t> delegate.
这篇关于C#传递单个方法作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!