转换动作< T>动作< object> [英] Convert Action<T> to Action<object>

查看:48
本文介绍了转换动作< T>动作< object>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困住了.

如何转换 Action< T>到C#中的Action< Object> ?

问候马格努斯

推荐答案

以下是您要求的示例(可以在最后一行中添加类型检查,以正确处理无效的强制转换异常,从而更加人性化):

Here's a sample of what you ask for (type check can be added in last line to properly handle invalid cast exception to be more user-friendly):

public Action<object> Convert<T>(Action<T> myActionT)
{
    if (myActionT == null) return null;
    else return new Action<object>(o => myActionT((T)o));
}

也许您可以提供有关该任务的更多详细信息,因为现在看起来有点奇怪.

May be you can give more details about the task though, because right now it looks a bit odd.

这篇关于转换动作&lt; T&gt;动作&lt; object&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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