Using Reflection to analyze Parameters and their values

天涯浪子 提交于 2019-12-24 05:34:20

问题


I've seen older posts here on SO, about one year old which would mean that they do not really cover .NET 4 or maybe even 3.5 on this topic. So here goes.

If you with reflection were to fetch parameters for the current method

ParameterInfo[] methodParams = MethodInfo.GetCurrentMethod().GetParameters();

Looping through each parameter will let you fetch the parameter-name however, there is only a "DefaultValue" which I guess is there because of the new Dynamic Parameters in .NET 4.

However, my question is; Is it still not possible to get the method parameter values without digging into the debugger API?

I know that there might be a design flaw if you even need to consider using this.


回答1:


It is not possible to get the current parameter values without using the Profiling API.

MethodInfo objects are per-method, not per-call. There is no way to connect a MethodInfo with a given stack frame.

In addition, in Release builds, the parameter locals can be optimized out, so the values to not necessarily exist.

The DefaultValue property can be non-null in VB parameters, which already supports default values.



来源:https://stackoverflow.com/questions/2147368/using-reflection-to-analyze-parameters-and-their-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!