从一个匿名方法调用一个带有ref或out参数的方法 [英] Calling a method with ref or out parameters from an anonymous method

查看:159
本文介绍了从一个匿名方法调用一个带有ref或out参数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于从一个匿名方法调用一个带有ref或out参数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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