从一个匿名方法调用一个带有ref或out参数的方法方法、参数、ref、out

2023-09-03 04:11:13 作者:麤暴牀伖

这个问题是关系到一个我问有一天,我得到了一些很好的有益从答案

This question is related to one I asked the other day which I got some good helpful answers from.

我需要调用各种Web方法具有不同特征的通用方法。我希望能够在Web方法传递给一个方法,它有一个委托的说法,但我不知道该如何应对不同的签名。该解决方案是使用lambda表达式(或匿名的方式为我使用C#2的时刻)。

I needed to call various web methods with varying signatures in a generic way. I wanted to be able to pass the web method to a method which had a delegate argument but I was unsure how to deal with the varying signatures. The solution was to use lambdas (or anonymous methods as I'm using C#2 at the moment).

这很好地工作,直到我需要我的匿名方法来调用Web方法out参数。你不能的原因这里解释这样做。

This worked nicely until I needed my anonymous method to call a web method with out parameters. You can't do this for reasons explained here.

所以我的问题是,比没有ref或OUT参数,从我的匿名方法调用创建一个包装方法等,有没有更简单的方式做到这一点?

So my question is, other than creating a wrapper method with no ref or out params to call from my anonymous method, is there a easier way to accomplish this?

推荐答案

其实,你的可以的使用 REF - 只是没有直接与调用方法的参数;你可以,但是,就在值之前和之后调用复制:

Actually, you can use ref and out - just not directly with the calling method's parameters; you can, however, just copy the values before and after invoking:

static void Foo(ref string s, out int i)
{
    string tmpS = s;
    int tmpI = 0; // for definite assignment
    DoIt(delegate { Bar(ref tmpS, out tmpI); });
    s = tmpS;
    i = tmpI;
}
 
精彩推荐
图片推荐