在System.Servicemodel.ClientBase <>。ChannelBase <> EndInvoke中使用out参数

时间:2019-09-09 10:04:35

标签: c# invoke out

This is the official page of the method,我想我需要使用'out'参数作为参数。我有这样的方法:

bool GetInfo(out List<foo> bar);

如何在不更改调用的方法的情况下使用out参数?在这种情况下,我真的不想返回bool和List的集合。

到目前为止,我已经尝试过像这样使用它:

EndInvoke("GetInfo", new object[] { out bar }, ...);
EndInvoke("GetInfo", new object[] { bar }, ...);
EndInvoke("GetInfo", new object[] { }, ...);
EndInvoke("GetInfo", null, ...);
List<foo> bar = (List<foo>)EndInvoke("GetInfo", new object[] { bar }, ...);

但是没有什么真正意义上的,对象数组内的out条显然根本不起作用。

1 个答案:

答案 0 :(得分:0)

显然,您只是忽略了out参数。这是我目前的工作解决方案:

public bool GetInfo(out List<foo> bar) {
   bar = new List<foo>();
   object[] args = new object[] { bar };
   IAsyncResult result = BeginInvoke("GetInfo", args, null, null);
   EndInvoke("GetInfo", args, result);
   bar = (List<foo>)args[0];
   return true;
}

Invokes调用使用问题中给出的接口行中的GetInfo方法。它们是使用相同名称的2个独立方法,因此,如果有人看到此方法并认为它可以帮助他们,请不要对此感到困惑。