2

I am wondering how I achieve something like this?

Action<int>  test = (val) => Console.WriteLine("I am " + val);
Action<object> test2 = (Action<object>) test;

code : https://dotnetfiddle.net/rZYvLZ

I need to do this because I am creating a class that will need to support many (thousands) of Actions delegate argument types

dgamma3
  • 2,333
  • 4
  • 26
  • 47

1 Answers1

4

Well, you could wrap the first action:

Action<object> test2 = (object o) => test((int)o);

But I think it doesn't need much explanation this is a very unsafe thing to do. So instead of breaking type safety, maybe you can come up with a better design that does respect type safety.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325