我如何可以通过多种方法(包括参数)作为参数? [英] How can I pass several methods (with parameters) AS a parameter?

查看:121
本文介绍了我如何可以通过多种方法(包括参数)作为参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下的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(数据)作为一个法阵,并把结果返回无论是异步或同步。

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

我将如何做到这一点?

我想我可以有两个'裁判'对象来处理结果[]和一个共享锁的结果[]。但是我不知道如何传递与参数的方法作为参数传递给另一个函数。

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#动作委托,刚刚执行的动作,不返回任何值。

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



的结果是:

Result is:

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

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