什么是转换动作&LT的最佳途径; T>由FUNC< T,特雷斯>? [英] What is the best way to convert Action<T> to Func<T,Tres>?

查看:148
本文介绍了什么是转换动作&LT的最佳途径; T>由FUNC< T,特雷斯>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的类中的两个函数与此签名,

I have two functions in my class with this signatures,

public static TResult Execute<TResult>(Func<T, TResult> remoteCall);
public static void Execute(Action<T> remoteCall)

我如何能够通过相同的委托,在第二个方法的第一个?与委托参数创建方法不是这样,因为我失去了一些异常信息
非常感谢!

How can I pass the same delegate in the second method to the first one? Creating method with Delegate argument is not a way, because I am loosing some exception informations
Thanks a lot!

推荐答案

敷在键入 Func键℃的代表; T,TResult&GT; 用一个虚拟的返回值,例如:

Wrap it in a delegate of type Func<T, TResult> with a dummy return value, e.g.

public static void Execute(Action<T> remoteCall)
{
    Execute(t => { remoteCall(t); return true; });
}

这篇关于什么是转换动作&LT的最佳途径; T&GT;由FUNC&LT; T,特雷斯&GT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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