创建函数来循环访问 C# 中方法的属性?
我想要做的是创建一个函数,该函数将从 .cs 文件中的每个方法调用,然后获取传递给该方法的所有属性名称和值并创建它们的字符串。
这将用于调试日志,以显示错误发生时每个属性的值,作为每次发生错误时手动创建字符串的替代方法。
这可能吗?我查看了反射,我认为这是我所需要的,但我不确定是否将当前方法从自身传递到新函数中,然后在函数内部将方法的属性拉出(我假设这是反射发挥作用。)
What I am trying to do is create a function that will be called from each method within a .cs file to then get all of the property names and values that are passed to the method and create a string of them.
This will be used for a debug log to show what the value of each property was when the error occurred as an alternative to manually creating the string each time an error occurs.
Is this possible? I have looked at reflection and I think it is what I need but I am not sure about passing the current method from itself into the new function, and then inside of the function pulling the properties of the method out (I assume this is where the reflection comes into play.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种更简单的方法可能是捕获异常,然后仅记录您感兴趣的属性的值。尝试使用反射来实现您想要做的事情可能是过度设计。
A simpler approach might be to catch the exception, and then only log the values of the properties you're interested in. Trying to achieve what you want to do using reflection is possibly over-engineering.
怎么样:
编辑 - 抱歉,我似乎误读了这个问题。我不知道如何做你的要求,或者即使这是可能的。
How about something like:
edit - Sorry I seem to have misread the question. I have no idea how to do what your asking, or even if it's possible.