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

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

问题描述

假设我有以下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 ;

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();
    }
}

回应一些意见:

此代码编译并等效:

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天全站免登陆