简体   繁体   中英

How Can I Get Current Method's Name And Its Arguments(name and value) in C#

For Example In Javascript:

function logger(args) {
  console.log('Func "' + args.callee.name + '" invoked.');
  for (var arg in args) {
    console.log(arg); //well I cannot get those arguments' name in js
  }
}

function doSomething(x, y, z) {
  logger(this.arguments);
  //do something...
}

How can I do something similar to that in C#?

Actually, I was about to implement a Web Service Logger in my program. Any suggestion for that?

Thanks to all.

edited: Sorry I didn't make it clearer. I knew that System.Reflection.MethodBase.GetCurrentMethod() could get me the caller function's MethodInfo, but the arguments' value is what I concerned more.

method name:

void logger([CallerMemberName] string methodName = "") { ... }

when you call logger() in doSomething() , methodName should be 'doSomething' .

but is no way to get arguments.

see:

Can I get parameter names/values procedurally from the currently executing function?

you basicly cannot do this. unless you use AOP tool like PostSharp

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