Using Reflection to analyze Parameters and their v

2019-07-21 00:59发布

问题:

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.