C# 如何使用Action、Delegate或list执行方法列表?

C# 如何使用Action、Delegate或list执行方法列表?,c#,list,delegates,action,C#,List,Delegates,Action,假设在同一个类中有3个具有相同签名的方法 Void Method1(string a, string b) Void Method2(string a, string b) Void Method3(string a, string b) 我想以这种方式调用该方法: foreach(Item item in T) ( item(a,b); ) 做这件事的最佳方式是什么:行动、列表、委派等等 注意:我读过关于它们的文章,但不知道如何在这个特定场景中使用它假设您的实际方法确实返回void,您

假设在同一个类中有3个具有相同签名的方法

Void Method1(string a, string b)
Void Method2(string a, string b)
Void Method3(string a, string b)
我想以这种方式调用该方法:

foreach(Item item in T)
(
  item(a,b);
)
做这件事的最佳方式是什么:行动、列表、委派等等


注意:我读过关于它们的文章,但不知道如何在这个特定场景中使用它

假设您的实际方法确实返回void,您可以使用

Action-Action=Method1;
行动+=方法2//将委托升级为多播委托
行动+=方法3;
行动(“x”、“y”);
如果您的方法返回其他内容,并且确实希望访问所有三个返回值,则不应使用多播委托。例如,如果方法返回int,则
inti=action(“x”,“y”)
将只捕获要调用的最后一个方法的返回值

在这种情况下,应该分别调用每个方法


注意:既然你说了“Action,Delegate或List”,我觉得有必要指出这是一个委托。

假设你的实际方法确实返回void,你可以使用

Action-Action=Method1;
行动+=方法2//将委托升级为多播委托
行动+=方法3;
行动(“x”、“y”);
如果您的方法返回其他内容,并且确实希望访问所有三个返回值,则不应使用多播委托。例如,如果方法返回int,则
inti=action(“x”,“y”)
将只捕获要调用的最后一个方法的返回值

在这种情况下,应该分别调用每个方法


注意:既然您说了“Action、Delegate或List”,我觉得有必要指出这是一个委托。

您可以使用
Delegates
调用具有相同签名的多个方法

试试这个:

        public delegate void myDelegate(string a, string b);


        myDelegate delMethods = new myDelegate(method1);
        delMethods+=new myDelegate(method2);
        delMethods+= new myDelegate(method3);
        delMethods("a","b");

您可以使用
委托
调用具有相同签名的多个方法

试试这个:

        public delegate void myDelegate(string a, string b);


        myDelegate delMethods = new myDelegate(method1);
        delMethods+=new myDelegate(method2);
        delMethods+= new myDelegate(method3);
        delMethods("a","b");

如果要以指定的方式调用方法,请执行以下操作:

var methods = new List<Action<string, string>> { 
    Method1,
    Method2,
    Method3
};

foreach (var method in methods) {
    item(a, b);
)
var方法=新列表{
方法1,
方法2,
方法3
};
foreach(方法中的var方法){
项目(a、b);
)

如果要以指定的方式调用方法,请执行以下操作:

var methods = new List<Action<string, string>> { 
    Method1,
    Method2,
    Method3
};

foreach (var method in methods) {
    item(a, b);
)
var方法=新列表{
方法1,
方法2,
方法3
};
foreach(方法中的var方法){
项目(a、b);
)

您可以编写如下帮助器方法:

public static void Call<T1, T2>(T1 arg1, T2 arg2, params Action<T1, T2>[] actions)
{
    foreach (var action in actions)
        action(arg1, arg2);
}
publicstaticvoid调用(T1 arg1,t2arg2,params Action[]actions)
{
foreach(行动中的var行动)
行动(arg1、arg2);
}
然后你可以这样使用它:

using System;

namespace Demo
{
    internal class Program
    {
        private void run()
        {
            Call("a", "b", method1, method2, method3);

            // You can pass as many methods as you need:

            Call("a", "b", method1, method2, method3, method1, method2, method3);
        }

        public static void Call<T1, T2>(T1 arg1, T2 arg2, params Action<T1, T2>[] actions)
        {
            foreach (var action in actions)
                action(arg1, arg2);
        }

        private static void method1(string a, string b)
        {
            Console.WriteLine("method1(), a = " + a + ", b = " + b);
        }

        private static void method2(string a, string b)
        {
            Console.WriteLine("method2(), a = " + a + ", b = " + b);
        }

        private static void method3(string a, string b)
        {
            Console.WriteLine("method3(), a = " + a + ", b = " + b);
        }

        private static void Main()
        {
            new Program().run();
        }
    }
}
使用系统;
名称空间演示
{
内部课程计划
{
私家车
{
调用(“a”、“b”、方法1、方法2、方法3);
//您可以根据需要传递任意多个方法:
调用(“a”、“b”、方法1、方法2、方法3、方法1、方法2、方法3);
}
公共静态无效调用(T1 arg1、T2 arg2、params Action[]actions)
{
foreach(行动中的var行动)
行动(arg1、arg2);
}
私有静态void方法1(字符串a、字符串b)
{
Console.WriteLine(“method1(),a=“+a+”,b=“+b”);
}
私有静态void方法2(字符串a、字符串b)
{
Console.WriteLine(“method2(),a=“+a+”,b=“+b”);
}
私有静态void方法3(字符串a、字符串b)
{
Console.WriteLine(“method3(),a=“+a+”,b=“+b”);
}
私有静态void Main()
{
新程序().run();
}
}
}

但是,我可能只会使用@dcastro提供的多播代理解决方案。

您可以编写如下帮助器方法:

public static void Call<T1, T2>(T1 arg1, T2 arg2, params Action<T1, T2>[] actions)
{
    foreach (var action in actions)
        action(arg1, arg2);
}
    public delegate void customDelegate(string a, string b);


    customDelegate methodsDelegate = new customDelegate(method1);
    methodsDelegate+=new customDelegate (method2);
    methodsDelegate+= new customDelegate (method3);
    methodsDelegate("a","b");
publicstaticvoid调用(T1 arg1,t2arg2,params Action[]actions)
{
foreach(行动中的var行动)
行动(arg1、arg2);
}
然后你可以这样使用它:

using System;

namespace Demo
{
    internal class Program
    {
        private void run()
        {
            Call("a", "b", method1, method2, method3);

            // You can pass as many methods as you need:

            Call("a", "b", method1, method2, method3, method1, method2, method3);
        }

        public static void Call<T1, T2>(T1 arg1, T2 arg2, params Action<T1, T2>[] actions)
        {
            foreach (var action in actions)
                action(arg1, arg2);
        }

        private static void method1(string a, string b)
        {
            Console.WriteLine("method1(), a = " + a + ", b = " + b);
        }

        private static void method2(string a, string b)
        {
            Console.WriteLine("method2(), a = " + a + ", b = " + b);
        }

        private static void method3(string a, string b)
        {
            Console.WriteLine("method3(), a = " + a + ", b = " + b);
        }

        private static void Main()
        {
            new Program().run();
        }
    }
}
使用系统;
名称空间演示
{
内部课程计划
{
私家车
{
调用(“a”、“b”、方法1、方法2、方法3);
//您可以根据需要传递任意多个方法:
调用(“a”、“b”、方法1、方法2、方法3、方法1、方法2、方法3);
}
公共静态无效调用(T1 arg1、T2 arg2、params Action[]actions)
{
foreach(行动中的var行动)
行动(arg1、arg2);
}
私有静态void方法1(字符串a、字符串b)
{
Console.WriteLine(“method1(),a=“+a+”,b=“+b”);
}
私有静态void方法2(字符串a、字符串b)
{
Console.WriteLine(“method2(),a=“+a+”,b=“+b”);
}
私有静态void方法3(字符串a、字符串b)
{
Console.WriteLine(“method3(),a=“+a+”,b=“+b”);
}
私有静态void Main()
{
新程序().run();
}
}
}
然而,我可能只会使用@dcastro给出的多播代理解决方案

    public delegate void customDelegate(string a, string b);


    customDelegate methodsDelegate = new customDelegate(method1);
    methodsDelegate+=new customDelegate (method2);
    methodsDelegate+= new customDelegate (method3);
    methodsDelegate("a","b");
无论订阅的顺序如何,调用方法的顺序都是相同的

但是在做这件事之前,你应该正确地阅读和理解代表,因为不把这个问题放在StackOverFlow上是完全可以实现的

无论订阅的顺序如何,调用方法的顺序都是相同的

但是在做这件事之前,你应该正确地阅读和理解代表,因为不把这个问题放在StackOverFlow上是完全可以实现的