简体   繁体   English

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

[英]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.

相关问题 如何通过名称获取方法处理程序? (C#) - How can I get a method handler by its name? (c#) 如何在C#中获取当前(WLAN)网络的名称? - How can I get the name of the current (WLAN) network in C#? C# - 如何获取互斥锁的所有者名称 - C# - how can I get owner's name for a Mutex 如何在c#中解析JSON字符串获取错误无法反序列化当前的JSON对象(例如{“name”:“value”}) - How can i parse JSON string in c# Get error Cannot deserialize the current JSON object (e.g. {“name”:“value”}) 如何将对应的变量名称获取为其对应的值C# - How to get the corresponding variable name to its corresponding value C# 如何从对象中获取属性和值的名称并将此名称和值传递给 c# 中的列表 - How can i get the name of property and value from object and pass this name and value to a list in c# 当我在 C# 中有多个 datagridviews 时,如何获取当前选定的 datagridview 的名称? - How can I get the name of the current selected datagridview when I’ve multiple datagridviews in C#? 如何获取 C# 中当前可执行文件的名称? - How do I get the name of the current executable in C#? C#方法“方法”没有重载接受0个参数,名称“名称”在当前上下文中不存在 - c# no overload for method “method” takes 0 arguments, the name “name” does not exist in the current context 如何在WinForm C#中获取当前正在运行的应用程序名称和ID? - How can i get Current running Application name and ID in WinForm C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM