Getting a variable value by name

Hi,

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() + ";";
                  }
            }
     
      }
}

Thanks

Chris
chrispontAsked:
Who is Participating?
 
gregoryyoungCommented:
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 {
                base.SomeMethod(foo);
          }
          catch(Exception Ex) {
                CallContext Context = BuildCallContextIncludingParameters();
                MyExceptionHandler.HandlerError(Context);
          }
    }
}

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) ...

0
 
strickddCommented:
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.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
gregoryyoungCommented:
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) http://msdn2.microsoft.com/en-us/library/system.reflection.propertyinfo.getvalue.aspx

0
 
Bob LearnedCommented:
Greg,

Does that work with ParameterInfo?

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

Bob
0
 
gregoryyoungCommented:
sorry read Property not parameter ...

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

use an exception handling aspect on your service object ... AOP http://en.wikipedia.org/wiki/Aspect-oriented_programming 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)
0
 
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#?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.