Getting method parameters as an object [] in C #

Is there any dark, hidden way to convert all method parameters to an object []?

When introducing integration between the two systems using a message broker, I noticed that most of the methods open by the broker use many parameters.

I want an easy way to log every broker call with every parameter. Something like:

[WebMethod] public void CreateAccount(string arg1, int arg2, DateTime arg3, ... ) { object[] args = GetMethodArgs(); string log = args.Aggregate("", (current, next) => string.Format("{0}{1};", current, next)); Logger.Log("Creating new Account: " + args); // create account logic } 

I am wondering if C # provides something that mimics GetMethodArgs ();

+1
c # log4net
source share
3 answers

You can have only 2 methods.

 [WebMethod] public void CreateAccount(string arg1, int arg2, DateTime arg3) { CreateAccountImpl(arg1, arg2, arg3); } protected void CreateAccountImpl(params object[] args) { string log = args.Aggregate("", (current, next) => string.Format("{0}{1};", current, next)); Logger.Log("Creating new Account: " + args); // create account logic } 
+2
source share

PostSharp can capture this using the method boundary aspect. Here is a sample code to see it in action.

 public sealed class Program { public static void Main() { Go("abc", 234); } [ParamAspect] static void Go(string a, int b) { } } [Serializable] public class ParamAspect : OnMethodBoundaryAspect { public override void OnEntry(MethodExecutionArgs args) { object[] argumentContents = args.Arguments.ToArray(); foreach (var ar in argumentContents) { Console.WriteLine(ar); } } } 

Output:

 abc 234 
+1
source share

For logging / auditing, I used Castle.DynamicProxy to port the web service implementation. All method calls are intercepted and passed to the IInterceptor object, which has access to the parameters and the return value.

0
source share

All Articles