使用带有新Func 的void返回类型
我在我的代码中使用匿名委托调用此示例函数:
public static int TestFunction(int a, int b) { return a + b; }
代表看起来像这样:
var del = new Func(TestFunction);
我的问题是:你如何为TResult
指定一个void
返回类型? 以下不起作用:
public static void OtherFunction(int a, string b) { ... } var del = new Func(OtherFunction);
如果没有返回类型,则需要Action
:
var del = new Action(OtherFunction);
要不就:
Action del = OtherFunction;
如果要返回void,则必须使用Action
如果您不想返回某些内容,则需要Action
如果您不需要任何返回值,请使用Action而不是Func
public void InvokeService(Binding binding, string endpointAddress, Action invokeHandler) where T : class { T channel = FactoryManager.CreateChannel (binding, endpointAddress); ICommunicationObject communicationObject = (ICommunicationObject)channel; try { invokeHandler(channel); } finally { try { if (communicationObject.State != CommunicationState.Faulted) { communicationObject.Close(); } } catch { communicationObject.Abort(); } } }