0

I want to use any method as a parameter of a method that cares about exception handling, something like this:

public void Run(){
    string result1 = (string)HandlingMethod(GiveMeString, "Hello");
    int result2 = (int)HandlingMethod(CountSomething, 1, 2);
}

public object HandlingMethod(something method, manyDifferentTypesOfParameters...){
    try{
        return method(manyDifferentTypesOfParameters)
    }catch(Exception ex){
        ....
    }
}

public string GiveMeString(string text){
    return text + "World";
}

public int CountSomething(int n1, int n2){
    return n1 + n2;
}

Is it possible to do it in C# .Net?

EDIT:

I found this solution, but I'm not sure how safe and ok it is. What do you think?

public class Program
    {
        public static void Main(string[] args)
        {
            string result1 = (string)Test(new Func<string,string>(TestPrint), "hello");
            int result2 = (int)Test(new Func<int, int, int>(TestPrint2), 4, 5);
            Console.WriteLine(result1);
            Console.WriteLine(result2);
        }

        public static object Test(Delegate method, params object[] args){
            Console.WriteLine("test test");
            return method.DynamicInvoke(args);
        }

        public static string TestPrint(string text){
           return text;
        }

        public static int TestPrint2(int n1, int n2){
            return n1 + n2 +1;
        }
    }
Banana Cake
  • 1,172
  • 1
  • 12
  • 31
  • I don't think you can do such generic things. –  Sep 26 '19 at 11:51
  • 1
    _"I'm not sure how safe and ok it is"_ -- the example you found is the most efficient, as it creates a delegate that will call your method directly, with the arguments you provide. But, it is not as convenient as requiring the caller to wrap their call in an anonymous method (typically represented as a lambda expression). The latter avoids the need to explicitly create a delegate (e.g. by casting or using `new`), which makes the code a bit more readable/concise. – Peter Duniho Sep 30 '19 at 00:05

2 Answers2

0

You can pass delegates in C#.

There are two types:

Action don't have any return Value, Function do have return Value. The only Problem I see here, is that you need so specify the parameters of the delegate, when you write the Method. Of course you could pass object[] as Parameter, but I don't think it's a good idea

  • why dont you recommend me to use the object[]? – Banana Cake Sep 26 '19 at 11:48
  • I edited the question with one solution, what do you think about it? – Banana Cake Sep 26 '19 at 12:01
  • @BananaCake if you use an object[] as param, you loose basically all benefits of a statically typed language. You have to check, if you received enough arguments, if the arguments are not null, if the arguments are the correct type and you have to cast them to the according type. If you pass the wrong kind of type, you could receive random exceptions – Thomas Stachl Sep 26 '19 at 12:32
-2

You can create generic handling methods for every number of parameters:

    public RetType HandlingMethod<P1Type,RetType>(Func<P1Type, RetType> method, P1Type p)
    {
        return method(p);
    }

    public RetType HandlingMethod<P1Type, P2Type, RetType>(Func<P1Type, P2Type, RetType> method, P1Type p1, P2Type p2)
    {
        return method(p1, p2);
    }
crazyman
  • 979
  • 8
  • 8