转换动作< T>动作< object>

转换动作< 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.