如何在C#中获取当前方法的名称及其参数(名称和值)

时间:2015-06-30 06:25:35

标签: c#

例如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...
}

我怎样才能做与C#类似的事情?

实际上,我正准备在我的程序中实现Web Service Logger。有什么建议吗?

感谢所有人。

编辑:对不起,我没有说清楚。我知道System.Reflection.MethodBase.GetCurrentMethod()可以获得调用函数的MethodInfo,但是参数'价值是我更关心的问题。

2 个答案:

答案 0 :(得分:0)

方法名称:

logger()

当您在doSomething()中致电methodName时,'doSomething'应为next()

但无法获得参数。

答案 1 :(得分:0)

请参阅:

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

你基本上不能这样做。 除非您使用的是PostSharp

等AOP工具