Link to home
Start Free TrialLog in
Avatar of chrispont
chrispontFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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

Greg,

Does that work with ParameterInfo?

Bob
Avatar of chrispont

ASKER

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

Bob
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)
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#?
ASKER CERTIFIED SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial