When registering, you always fall into string literals.
I solved this well for properties, fields, and variables by passing Expression<Func<T>> expression (as explained here ), so you can do something like this:
public void Demo(string someArgument) { LogFrameWork.LogLine("Demo"); // goal is to get rid of these string literals LogFramework.Log(() => someArgument); }
I want to do something similar for the Demo method itself:
public void Demo(string someArgument) { LogFramework.Log(this.Demo); }
I tried things like this:
public static void Log(Delegate method) { string methodName = method.Method.Name; LogLine(methodName); }
and this:
public static void Log(Action method) { string methodName = method.Method.Name; LogLine(methodName); }
But I get compiler errors like this:
Argument 1: cannot convert from 'method group' to 'System.Delegate' Argument 1: cannot convert from 'method group' to 'System.Action'
I could introduce a bunch of overloads using Func <...> and Action <...> , but that sounds too complicated.
Is there any way to cover this for any method with any number of parameters and optional result?
- Jeroen
PS: I think this question may have some significance here, but there are no answers that made me feel aha :-)
methods c # parameters
Jeroen Wiert Pluimers
source share