Magnus Gladh Magnus Gladh - 1 year ago 81
C# Question

Convert Action<T> to Action<object>

I am stuck.

How do I convert the

Action<T> to an Action<Object>
in C#?


Answer Source

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.