如何将几种方法(带参数)作为参数传递? [英] How can I pass several methods (with parameters) AS a parameter?

查看:20
本文介绍了如何将几种方法(带参数)作为参数传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下 WCF 代码:

Suppose I have the following WCF code:

  try
  {
       ServiceClient proxy = new ServiceClient();
       proxy.ClientCredentials.UserName.UserName = "user";
       proxy.ClientCredentials.UserName.Password = "password";
       proxy.GetData(2);
       if (proxy.State = CommunicationState.Opened)
       {
           proxy.GetData("data");
       }
       proxy.Close();
  }
  catch (FaultException ex)
  {
      // handle the exception      
  }

而且由于我注意到 try...catch 和其他逻辑是重复的,更不用说设置 WCF 调用很昂贵,我想向这个函数发送许多方法和参数".

And since I notice that the try...catch and other logic is repetitive, not to mention that setting up a WCF call is expensive, I want to send many "methods and parameters" to this function.

本质上将 GetData(2)GetData("data") 作为方法数组传递,并让结果异步或同步返回.

In essence pass GetData(2) and GetData("data") as a method array, and have the results return either asynchronously or synchronously.

我将如何做到这一点?

我想我可以有两个 'ref' 对象来处理结果 [] 和结果 [] 的共享锁.但是我不确定如何将带参数的方法"作为参数传递给另一个函数.

I suppose I could have two 'ref' objects to handle the results[] and a shared lock to the results[]. However I'm not sure how to pass "methods with parameters" as a parameter to another function.

也许另一种看待它的方式可能是一个函数指针数组,指向具有不同参数的同一个函数.

Perhaps another way of looking at this might be an array of function pointers, to the same function with different params.

任何人都可以推动我采取正确的做法吗?

Can anyone nudge me into the right way of doing this?

更多信息:

我在问这个问题 所以我可以优化这种处理 WCF 异常和重试的方法,但我没有每次通话后必须始终打开/关闭客户端.

I am asking this question so I can optimize this approach to handling WCF exceptions and retries but so I don't have to always open/close the client after each call.

推荐答案

使用委托并在列表中传递它们.

需要返回值时使用 C# Func<T> 委托.

The C# Func<T> delegate is used when a return value is needed.

List<Func<Data>> funcList = new List<Func<Data>>();
funcList.Add( () => GetData(2) );

// You can use any condition as you otherwise would to add to the list.
if (proxy.State = CommunicationState.Opened)
{
   funcList.Add( () => GetData("data") );
}

List<Data> ProcessFuncs(List<Func<Data>> funcDatas)
{
    List<Data> returnList = new List<Data>();
    foreach(var func in funcDatas)
    {
        returnList.Add(func());
    }
}

(只要返回类型相同,这将起作用)

( as long as the return types are identical, this will work )

这当然只是一个例子;如果您的方法没有返回任何内容,您可以使用 C# Action 委托,它只执行一个操作并且不返回任何值.

This is just an example of course; if your methods don't return anything, you can use the C# Action delegate, which just executes an action and doesn't return any value.

List<Action> actionList = new List<Action>();
actionList.Add( () => ProcessData("data")); // ProcessData is a void with no return type
actionList.Add( () => ProcessData(2));

public void ProcessActions(List<Action> actions)
{
    foreach(var action in actions)
    {
        action();
    }
}

回应一些评论:

这段代码编译后完全等效:

This code compiles and is all equivalent:

class Program
{
    public static string GetData(string item) { return item; }
    public static string GetData(int item) { return item.ToString(); }

    static void Main(string[] args)
    {
        string someLocalVar = "what is it?";
        int someLocalValueType = 3;

        Func<string> test = () =>
        {
            return GetData(someLocalVar);
        };

        Func<string> test2 = () => GetData(someLocalValueType);
        someLocalValueType = 5;

        List<Func<string>> testList = new List<Func<string>>();

        testList.Add(() => GetData(someLocalVar));
        testList.Add(() => GetData(2));
        testList.Add(test);
        testList.Add(test2);

        someLocalVar = "something else";

        foreach(var func in testList)
        {
            Console.WriteLine(func());
        }

        Console.ReadKey();
    }
}

结果是:

这篇关于如何将几种方法(带参数)作为参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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