[英]How Can I Get Current Method's Name And Its Arguments(name and value) in C#
For Example In Javascript: 例如在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#? 我该如何做与C#类似的事情?
Actually, I was about to implement a Web Service Logger in my program. 实际上,我打算在程序中实现Web Service Logger。 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. 我知道
System.Reflection.MethodBase.GetCurrentMethod()
可以让我获得调用者函数的MethodInfo,但是参数的值是我最关心的问题。
method name: 方法名称:
void logger([CallerMemberName] string methodName = "") { ... }
when you call logger()
in doSomething()
, methodName
should be 'doSomething'
. 当您在
doSomething()
调用logger()
时, methodName
应该为'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
除非您使用PostSharp之类的 AOP工具
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.