0

I have a method that needs to take the function and its owner class as the input arguments and then to proceed with its names with reflection. Of course I could take a couple of strings as the input args, so the method would look like this:

void Proceed(string className, string methodName)
{
  Console.WriteLine($"{className}.{methodName} called");
  //...
}

and would be called like this:

Proceed(nameof(Foo), nameof(Foo.Bar));

But I wonder if there is any way to avoid writing the nameof keyword every time I am trying to call the method. To me something like Proceed<Foo>(f => f.Bar) would look a lot nicer.

I suppose this could be solved with expressions. The problem I face is that if method Bar has arguments, you have to specify them explicitly when calling the method (which seems excessive in my case where you only going to need the name of the method later).

So, the best solution I managed to find is this one:

void Proceed<T>(Expression<Func<T, Action<object, object>>> expression)
{
//...
}

Yet it still specifies the argument method's signature in its own args and therefore is not generic enough.

I wonder if there is some way to pass the function as an argument without specifying its arguments (provided I am only going to need its name later, just like nameof keyword does).

Community
  • 1
  • 1
bashis
  • 1,200
  • 1
  • 16
  • 35
  • https://msdn.microsoft.com/en-us/library/hh534540(v=vs.110).aspx – Hans Passant Jul 18 '16 at 17:39
  • Possible duplicate of [How to pass an arbitrary method (or delegate) as parameter to a function?](http://stackoverflow.com/questions/15931306/how-to-pass-an-arbitrary-method-or-delegate-as-parameter-to-a-function) – ManoDestra Jul 18 '16 at 17:48
  • Like @HansPassant mentioned, you can easily get the member name, but not the type name. I don't see any advantage on using the an expressing tree other than capturing `this`, but at an higher cost, You could write a Roslyn analyzer to ensure the proper arguments are being passed. – Paulo Morgado Jul 18 '16 at 19:39

2 Answers2

0

I think you are looking for the usage of an event or delegate.

You can look up more information regarding those 2 online. The idea is that they store some methods with the same arguments so that later it knows what kind of arguments you need to call.

prodrom
  • 1
  • 2
  • I think that it would help if you defined the terms here in the answer rather than simply suggesting that the original questioner look up the terms online. After all, StackOverflow.com is online. – MichaelDotKnox Jul 18 '16 at 18:26
-1

Yes, use the Delegate keyword.

See the answer here.

Community
  • 1
  • 1
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • If you're just going to link to another answer, why didn't you just flag it as a duplicate? – sstan Jul 18 '16 at 17:48
  • There is a difference between a duplicate question and a duplicate answer. See [here](http://meta.stackoverflow.com/questions/313277/duplicate-answer-duplicate-question). – John Wu Jul 18 '16 at 18:08