Convert Action to Action<object>

前端 未结 4 864
感动是毒
感动是毒 2020-12-11 14:19

I am stuck.

How do I convert the Action to an Action in C#?

Regards Magnus

4条回答
  •  鱼传尺愫
    2020-12-11 15:26

    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 Convert(Action myActionT)
    {
        if (myActionT == null) return null;
        else return new Action(o => myActionT((T)o));
    }
    
    
    

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

    提交回复
    热议问题