如何查找函数参数
我需要记录十几个函数中的所有函数参数。
有没有一种方法可以通过编程来确定所有参数及其值(或至少确定它们的 .ToString() 值)?也许通过反思?
I need to log all the function parameters in a dozen functions.
Is there a way to pro grammatically determine all the parameters and their values (or at least their .ToString() value)? Perhaps via reflection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是如何使用 PostSharp 执行此操作的示例
http://consultingblogs.emc.com/merrickchaffer/archive/2009/08/04/using-postsharp-to-log-method-entry-and -exit-in-net-code.aspx
您也可以推出自己的
http://www.developerfusion.com/article/5307/aspect-orient-programming-using-net/3/
Here is an example of how to do this with PostSharp
http://consultingblogs.emc.com/merrickchaffer/archive/2009/08/04/using-postsharp-to-log-method-entry-and-exit-in-net-code.aspx
You can also roll your own
http://www.developerfusion.com/article/5307/aspect-oriented-programming-using-net/3/
据我所知,无法使用反射来动态列出和确定局部变量的值。您可以使用反射来获取有关方法参数的类型信息,但仅限于声明类型 - 您无法自动获取有关实际参数的信息,因为反射元数据提供有关方法定义的信息,而不是运行时传递给它的特定值。
但是,您可以执行以下操作:
然后,在要执行日志记录的每个方法中,执行类似的操作
To the best of my knowledge there's no way to use reflection to dynamically list and determine value of local variables. You can use reflection to get type information about the parameters of a method, but only the declared type - you can't automatically get information about the actual arguments, because the reflection metadata gives information about the method definition, not the specific values passed to it at runtime.
You can, however, do something like this:
Then, from within each method in which you want to perform logging, do something like