Getting values of parameters to method

  • Thread starter Thread starter Brad Quinn
  • Start date Start date
B

Brad Quinn

Is there a way to get the values of the paramaters to a
method programatically?

I know that I can use reflection to find out the
parameter names and types, etc., but I want to know the
values of those parameters too.

I'm trying to write a generic error handler. I wan't to
be able to log the values that caused the error, without
the user of the class having to specify the values 'cause
they're lazy ;)

Thanks,
Brad
 
Is there a way to get the values of the paramaters to a
method programatically?

I know that I can use reflection to find out the
parameter names and types, etc., but I want to know the
values of those parameters too.

I'm trying to write a generic error handler. I wan't to
be able to log the values that caused the error, without
the user of the class having to specify the values 'cause
they're lazy ;)

Thanks,
Brad

MethodBase.GetCurrentMethod() returns the method that you're inside right
now. From there you can move on to get all the params to it including
values.
 
Hi Brad,
Parameters have value only inside the body of the function (at run time).
Reflection won't help here.

B\rgds
100
 
StackFrame has a method named GetMethod() that returns a
MethodBase.

MethodBase has a method named GetParameters() that
returns a ParameterInfo [].

The problem is that ParameterInfo (understandably) does
not contain the values.

However, there must be references to the parameters on
the call stack. I should think that there might be a way
to get at them, possibly through the StackFrame.

I was hoping that ArgIterator might help, but I can't
figure out how to intialize one (don't know how to get a
RuntimeArgumentHandle).

Thanks anyway.
 
If you really want to do this, you received some good answers already.

I feel that this is bad architectural practice because it obscures what is
really happening in the error handlers, and it adds unnecessary complexity
to the code base.

The axim "Just because you can do something doesn't mean you should do it"
applies farily often to reflection - keep it simple.

Eric
 
I did a lot of research following this question, and there's no way to do it
using .net.

Only way to go is :
- hijack in the debugging sdk and attach a debugger to the process to get
the parameter values.
- intercept calls using message sinks and contextboumd objects
- know in advance your parameters.

Arguments are not on the "call stack" but on the "stack" itself, so you
can't really walk that stack yourself.

--
Sebastien Lambla
http://thetechnologist.is-a-geek.com/blog/


Brad Quinn said:
StackFrame has a method named GetMethod() that returns a
MethodBase.

MethodBase has a method named GetParameters() that
returns a ParameterInfo [].

The problem is that ParameterInfo (understandably) does
not contain the values.

However, there must be references to the parameters on
the call stack. I should think that there might be a way
to get at them, possibly through the StackFrame.

I was hoping that ArgIterator might help, but I can't
figure out how to intialize one (don't know how to get a
RuntimeArgumentHandle).

Thanks anyway.
-----Original Message-----
On Wed, 12 Nov 2003 10:29:22 -0800, Brad Quinn wrote:


MethodBase.GetCurrentMethod() returns the method that you're inside right
now. From there you can move on to get all the params to it including
values.
 
Thanks,

That's what I thought.
-----Original Message-----
I did a lot of research following this question, and there's no way to do it
using .net.

Only way to go is :
- hijack in the debugging sdk and attach a debugger to the process to get
the parameter values.
- intercept calls using message sinks and contextboumd objects
- know in advance your parameters.

Arguments are not on the "call stack" but on the "stack" itself, so you
can't really walk that stack yourself.

--
Sebastien Lambla
http://thetechnologist.is-a-geek.com/blog/


"Brad Quinn" <[email protected]> a écrit dans le message
de news: [email protected]...
StackFrame has a method named GetMethod() that returns a
MethodBase.

MethodBase has a method named GetParameters() that
returns a ParameterInfo [].

The problem is that ParameterInfo (understandably) does
not contain the values.

However, there must be references to the parameters on
the call stack. I should think that there might be a way
to get at them, possibly through the StackFrame.

I was hoping that ArgIterator might help, but I can't
figure out how to intialize one (don't know how to get a
RuntimeArgumentHandle).

Thanks anyway.
-----Original Message-----
On Wed, 12 Nov 2003 10:29:22 -0800, Brad Quinn wrote:


MethodBase.GetCurrentMethod() returns the method that you're inside right
now. From there you can move on to get all the params
to
it including


.
 
I'm trying to keep things as simple as possible.

Obviously, for any particular method I know the
parameters. Suppose I have a method like this;

public int Compute( int a, int b )
{
try {
return a * b;
} catch ( Exception ex ) {
Log.Write( ex, string.Format( "a={0}, b={1}", a,
b ) );
throw;
}
}

When an exception is thrown I log the exception and the
parameters to the method, ignoring the fact that (in
general) member variables may have contributed to the
exception.

Now suppose that the method is changed, but that the lazy
programmer forgets to update the logging statement

public int Compute( int a, int b, int c )
{
try {
return a * b * c;
} catch ( Exception ex ) {
Log.Write( ex, string.Format( "a={0}, b={1}", a,
b ) );
throw;
}
}

If we look at the logs, we might be confused as to why
the exception is being thrown. I'm trying to make error
logging simpler (for the consumer).

Thanks for your concern.
 
Back
Top