Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am stuck.

How do I convert the Action<T> to an Action<Object> in C#?

Regards Magnus

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
203 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...