Getting a variable value by name


I'm writing a piece of debugging code that will send an email to some developers if an exception is thrown and send them the values passed in. Is there any way to loop through a methods parameters and write out the name and value? I'm using reflection to get the parameters name and this seems to work well, but getting the value is a different story. Here's my code so far....

private void getParameters(string methodName)
      System.Type typeInfo = typeof(Service1);
      string TheValues = "";
      foreach (System.Reflection.MethodInfo mi in typeInfo.GetMethods())
            if(mi.Name == methodName)
                  foreach (System.Reflection.ParameterInfo pi in mi.GetParameters())
                        TheValues = TheValues + pi.Name.ToString() + ";";


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

At my work we created a custom error handler class that will intercept exceptions thrown in the program and send the e-mail. All you really need to do is a "for each" loop to get all the controls of a specific type and add their ID/Name and value properties to a string to send as an e-mail.
You need an instance in order to get the values ...

Once you have an instance (the instance causing the problem)

you can call ... PropertyInfo.GetValue(instance)

Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Bob LearnedCommented:

Does that work with ParameterInfo?

chrispontAuthor Commented:
No, I already tried that and ParameterInfo has no GetValue member
Bob LearnedCommented:
Right ;)  That was my point exactly *GRIN*

sorry read Property not parameter ...

I will therefor give an answer to the question ...

use an exception handling aspect on your service object ... AOP will solve this quite easily ...

naspect, and aspect# are too good frameworks to look at ... basically you will have an interceptor that groups up your parameters and sends them over ..

essentially it would dynamically generate the code you would write by hand to convert your parameters into a an object [] to pass them to the handling code. You could then based on the methodinfo/parameterinfo give back the names.

I can show an example if you like though it will not be a quick copy/paste function.

Also if you drop me an email this name at this site I can send you all of my slides from my presentation on this subject saturday (about 70)
chrispontAuthor Commented:
Thanks for all your comments.

Hmmm. Looks very interesting. Never heard of AOP before, but I kinda understand how it works.

I probably should have mentioned that part of the code that will use this is a Web Service which will need to inherit System.Web.Services so cannot inherit System.Attribute?

I guess what I really needed was a way to get a variable's value by passing a name into a function i.e. getValue(pi.Name.ToString()).

Is there anyway to achieve this in C#?
I am completely lost on your System.Atrribute comment ... what sort of AOP are you talking about?

based upon this bit of your original code ..

public class YourOriginalClass : System.Web.Services {
    public virtual void SomeMethod(int foo) {

imagine that it is automatically writing the following code for you.

public class YourOriginalClassProxy : YourOriginalClass {
    public overrides void SomeMethod(int foo) {
          try {
          catch(Exception Ex) {
                CallContext Context = BuildCallContextIncludingParameters();

the key being the code is written for you at run time by the aspect framework ... The Context object here would contain items for you such as the class being called ... the parameters passed etc. This context type behavior already exists in most AOP frameworks

Take a look at the caching example provided as a sample with naspect ... take a look at what the caching aspect is doing ... you would deal with the context slightly differently but it is similar.

You cannot possibly do what you are discussing (not on parameters anyway) ...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.