By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,979 Members | 960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,979 IT Pros & Developers. It's quick & easy.

Getting values of parameters to method

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
100
Hi Brad,
Parameters have value only inside the body of the function (at run time).
Reflection won't help here.

B\rgds
100

"Brad Quinn" <br********@yahoo.com> wrote in message
news:01****************************@phx.gbl...
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

Nov 15 '05 #2

P: n/a
On Wed, 12 Nov 2003 10:29:22 -0800, Brad Quinn wrote:
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.
--
Roy Osherove
weblog: http://www.iserializable.com
Nov 15 '05 #3

P: n/a
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 rightnow. From there you can move on to get all the params to it includingvalues.
--
Roy Osherove
weblog: http://www.iserializable.com
.

Nov 15 '05 #4

P: n/a
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

"Brad Quinn" <br********@yahoo.com> wrote in message
news:01****************************@phx.gbl...
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


Nov 15 '05 #5

P: n/a
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" <an*******@discussions.microsoft.com> a écrit dans le message
de news: 00****************************@phx.gbl...
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.
--
Roy Osherove
weblog: http://www.iserializable.com
.

Nov 15 '05 #6

P: n/a
Thanks,

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

Only way to go is :
- hijack in the debugging sdk and attach a debugger to the process to getthe 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 youcan't really walk that stack yourself.

--
Sebastien Lambla
http://thetechnologist.is-a-geek.com/blog/
"Brad Quinn" <an*******@discussions.microsoft.com> a écrit dans le messagede news: 00****************************@phx.gbl...
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.
>--
>Roy Osherove
>weblog: http://www.iserializable.com
>.
>

.

Nov 15 '05 #7

P: n/a
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.
-----Original Message-----
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 isreally happening in the error handlers, and it adds unnecessary complexityto 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

"Brad Quinn" <br********@yahoo.com> wrote in message
news:01****************************@phx.gbl...
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


.

Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.