[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Getting a variable value by name

Posted on 2006-04-24
12
Medium Priority
?
234 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:chrispont
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16526338
0
 
LVL 28

Expert Comment

by:strickdd
ID: 16526452
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
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 16526596
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 16526774
Greg,

Does that work with ParameterInfo?

Bob
0
 

Author Comment

by:chrispont
ID: 16526786
No, I already tried that and ParameterInfo has no GetValue member
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16526804
Right ;)  That was my point exactly *GRIN*

Bob
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 16527043
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
 

Author Comment

by:chrispont
ID: 16532436
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
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 2000 total points
ID: 16532702
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question