1

I'm in a case where I would like to have a method that can take other methods as input.

The input method will always have a single object as input, but the objects are different.

This is more or less what i need to be able to do:

public static void takeAnything(Action<object> inputFunc)
{
    Console.WriteLine(inputFunc.Method.Name);
}

public static void test1(MyOwnObject input){
    // Do stuff with input object
}

public static void test2(MyOtherOwnObject input){
    // Do stuff with input object
}

public static void startSystem(){
    takeAnything(test1);
    takeAnything(test2);
}

Which in this dummy case would write out:

test1
test2

I just cant't get this to work, so any help would be very much appreciated.

EDIT

I do not know if this is possible, maybe it is not, but it is important that the call is just takeAnything(test1), not takeAnything<MyOwnObject>(test1) or anything else.

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
Bjarkes
  • 99
  • 1
  • 2
  • 8
  • 3
    What exactly is the problem you are having? Have you consindered making `TakeAnything` into a generic method (ie `TakeAnything(Action inputFunc`)? – Chris Feb 20 '14 at 14:07
  • I have tried that but I can only get it to work calling it with: takeAnything(test), and if I remove the part i get this error: The type arguments for method 'takeAnything(System.Action)' cannot be inferred from the usage. Try specifying the type arguments explicitly. I do not know if this is even possible, that is what I hope you guys can help me figuring out :) – Bjarkes Feb 20 '14 at 14:49

3 Answers3

5

Make your method generic

public static void TakeAnything<T>(Action<T> inputFunc)
{
    Console.WriteLine(inputFunc.Method.Name);
}

Of course, you will be able only to pass methods which have single input argument.

UPDATE: Unfortunately C# cannot infer action generic parameter type from usage, when you are passing method group to method, so only way to use it is specifying generic parameter type manually

takeAnything<MyOwnObject>(test)

For details see question C# 3.0 generic type inference - passing a delegate as a function parameter

Community
  • 1
  • 1
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0

Try this, so you can pass dynamic parameters to action:

public static void takeAnything(Expression<Action> inputFunc)
{
    var currentMethod = ((MethodCallExpression)inputFunc.Body);
    Console.WriteLine(currentMethod.Method.Name);
}

public static void test(MyOwnObject input){
    // Do stuff with input object
}

public static void startSystem(){
    MyOwnObject  yourObject = new MyOwnObject();
    takeAnything(() => test(yourObject));
}
Only a Curious Mind
  • 2,807
  • 23
  • 39
  • 1
    -1, did you even try to run this code? How do you think the lambda will be represented in the delegate? No, it won't magically disappear so that `Method.Name` could resolve to `"test"`. – Ondrej Tucny Feb 20 '14 at 14:12
  • 1
    Seems to work now (with the typo correction that the parameter name in `takeAnything` doesn't match the use in the first line (`action` vs `inputFun`). – Chris Feb 20 '14 at 14:34
  • Sorry, I edited my answer. Now I think was correctly – Only a Curious Mind Feb 20 '14 at 14:44
  • Thank you for the answer! For the solution I'm working on, all that creating an object and stuff is not good. If it is even possible I would like to be able to just do: takeAnything(test). But maybe it is not possible at all :) – Bjarkes Feb 20 '14 at 14:46
  • @Bjarkes To be able to do only `takeAnything(test)`, your action can't have any parameter. – Guilherme Oliveira Feb 20 '14 at 14:50
  • 2
    Removing the -1; however, the drawback of this solution still is one need to supply some (possibly dummy) arguments. I think a reasonable number of overloads with `Action` is a slightly better option. – Ondrej Tucny Feb 20 '14 at 14:51
-1

Why not change the header of your takeAnything method to:

public static void takeAnything(Action<MyOwnObject> inputFunc)
csteinmueller
  • 2,427
  • 1
  • 21
  • 32
  • 2
    Because the takeAnything method will be called with many different input methods, having different input objects. – Bjarkes Feb 20 '14 at 14:20