简体   繁体   中英

Is it possible to simply pass a generic method with generic parameters as an argument to another method in C#?

For a test, I want to create a generic "helper" method which will take take two arguments, the first argument is a function (or a reference to the function) and the 2nd argument is a list of objects for that function that are to be called as its parameters.

The following does this perfectly:

CallMyFunctionWithParamsPlease(new Func<int, int>(MyMethod), new object[] {1});

public static int CallMyFunctionWithParamsPlease(Delegate func, params object[] args)
{
  func.DynamicInvoke(args);
  return 3;
}

The thing is, this doesn't look very nice when calling it and I wish to abstract it into another method to act as syntatic sugar.

Ideally I want it to be called like this:

CallMyFunctionWithParamsPlease(myMethod, new Object[] {1});

From what I can gather, there is no elegant solution to do this in C# since I cannot pass myMethod by itself as a reference anywhere, instead I must pass it by declaring a new Func along with the return type of the method. Since I'm not using this return type anywhere, I'm not sure why it's necessary to input this information. My limited understanding is that because C# is statically typed, the compiler must know everything and things like this just aren't possible.

Is this true or not? How would I create syntatic sugar to simply pass a method to another method which can be called there without needing to invoke "new Func"? I would have thought simply passing the function as a reference pointer would allow me to do this, but I'm having difficultly doing this too. I looked into delegates, using "unsafe" with pointers, and a few other options. None of them seem to make this possible, or if they do, they didn't explain it in a manner that I could understand.

I simply want to pass a method to another method, and invoke it with a variable list of object params with variable length whereby I don't need to specify this whilst invoking it. I'm not sure if I'm trying to force C# to do something it's not meant to do here, and instead I'd be better off using a dynamically typed language to do this. The problem is I really enjoy the intellisense that the static typing of C# offers, along with the performance improvements over a language like Python. I'd just like a way to syntactically abstract away the boilerplate with my own helper methods for things like this.

UPDATE: Thanks to the comments here it seems I can do this with a lambda expression nice and elegantly. The signature can be simply changed to public static long CallMyFunctionWithParamsPlease<T>(Func<T> func)

If deferred execution is what you want simply pass a Func<TReturnType> to your method (or class). The calling method doesn't need to know how many parameters are involved.

eg Assuming MyMethod has a signature int MyMethod(int arg) :

CallMyFunctionWithParamsPlease(() => MyMethod(1));

public static int CallMyFunctionWithParamsPlease(Func<int> func)
{
   return func();
}

If MyMethod takes two parameters, it's the same call:

CallMyFunctionWithParamsPlease(() => MyMethod(1, 2));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM