C# 从操作委托C调用方法获取结果#
有没有办法从泛型操作委托中的invoke方法获取结果 代码执行C# 从操作委托C调用方法获取结果#,c#,delegates,inversion-of-control,C#,Delegates,Inversion Of Control,有没有办法从泛型操作委托中的invoke方法获取结果 代码执行 公共字符串TestRun() { ExecuteService(e=>e.ExecuteMethod(),out var result); 返回结果;//返回值; } 类方法 公共类SomeClass { 公共字符串ExecuteMethod() { 返回“你好!?”; } } 执行一般操作委托的方法 protected internal void ExecuteService(操作-操作,输出-响应),其中TAction:ne
公共字符串TestRun()
{
ExecuteService(e=>e.ExecuteMethod(),out var result);
返回结果;//返回值;
}
类方法
公共类SomeClass
{
公共字符串ExecuteMethod()
{
返回“你好!?”;
}
}
执行一般操作委托的方法
protected internal void ExecuteService(操作-操作,输出-响应),其中TAction:new()
{
action?.Invoke(new TAction());//Invoke
response=action?.something()//问题……如何从此点向前获取值
}
如何从action delegateExecuteService
方法内的方法ExecuteMethod()
获取返回值,并将其分配给out
值?这是可以实现的吗?您可以通过不使用void
,即无。如果需要委托返回值,请使用或如果需要它专门返回布尔值。像这样:
受保护的内部void ExecuteService(Func操作,out响应)
{
响应=操作?.Invoke();
}
如果您需要
操作
的内部函数来获取参数,请使用另一个Func
类,比如一个获取1个参数并返回T
的类。您好,我尝试了您的解决方案,但没有成功,它在操作?
中给了我一个错误。Invoke(),还有,我如何在invoke方法中注入我的IoC?多亏了这一点,我现在有了主意,我将的返回类型从void
更改为TOutput
<代码>受保护的内部TOutput ExecuteService(函数操作)。你信息丰富的评论真的帮了我很大的忙。非常感谢。