views:

51

answers:

3

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  A: 

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.

SLaks
Is there a huge performance loss in doing that? Do you need to compile the project for debugging?
Filip Ekberg
A huge performance loss in doing what?
SLaks
Using the Profiling API to get the value and not compiling in release-mode. It feels like it's not worth it.
Filip Ekberg
Correct, especially because an application can't profile itself, so you'd need two EXE files.
SLaks
A: 

in a for loop on methodParams

myMethod.GetParameters().GetValue(i);

F.Aquino
This will call `Array.GetValue` and return a `ParameterInfo` object; it will not give you the value of the parameter.
SLaks
Thanks for the correction SLaks! I was doing some validation with reflection and came across the GetValue method and just assumed it was for that end.
F.Aquino
A: 

I think, I answered a very similar question not so long ago: http://stackoverflow.com/questions/2062883/is-there-a-way-to-enumerate-passed-method-paramters/2066654#2066654

As you said, not without design flaws, though.

Alexandra Rusina