473,398 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Getting values of parameters to method

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
7 3652
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marcin | last post by:
Hello! Is there any method to detect parameters values passed to called method? For example: public Guid ApplicationLogin(string userName, string password, int dbId)
1
by: Julia | last post by:
Hi, I have been asked this before but I think that I didn't explain myself well I am using exception and logging and I would like to log the parameters which was passed to the function...
4
by: Mike Dinnis | last post by:
Hi, I've been working through a number of turorials to try to learn more about retrieving data from a SQL database. I think i've mastered techniques where i create a sql string in the page and...
2
by: Martin Raychev | last post by:
Hi all, I have the following problem: I have a private method that returns a SqlDataReader. For this to work I have not to close the DB connection in the above method. I do this only to
1
by: kplkumar | last post by:
I am trying to get the method and the parameters passed to that method from the stackTrace. My code is, private void GetExecutingMethodInfo() { StackTrace trace = new StackTrace(false); ...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
7
by: Aaron Gray | last post by:
I put together the following code to get the href's parameters :- function GetParameters() { var arg = new Object(); var href = document.location.href; if ( href.indexOf( "?") != -1) { var...
4
by: preeti13 | last post by:
Hi friends i have a probelm i am try to pass the value to the employeeid parameter but getting th error please help me how i can do this i am getting the error here is my code using System;...
2
by: preeti13 | last post by:
Hi guys i am here with my another probelm please help me.trying insert the value into the data base but getting the null value error .I am getting thsi error Cannot insert the value NULL into...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.