chrispont
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.MethodI nfo mi in typeInfo.GetMethods())
{
if(mi.Name == methodName)
{
foreach (System.Reflection.Paramet erInfo pi in mi.GetParameters())
{
TheValues = TheValues + pi.Name.ToString() + ";";
}
}
}
}
Thanks
Chris
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.MethodI
{
if(mi.Name == methodName)
{
foreach (System.Reflection.Paramet
{
TheValues = TheValues + pi.Name.ToString() + ";";
}
}
}
}
Thanks
Chris
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(inst ance) http://msdn2.microsoft.com/en-us/library/system.reflection.propertyinfo.getvalue.aspx
Once you have an instance (the instance causing the problem)
you can call ... PropertyInfo.GetValue(inst
Greg,
Does that work with ParameterInfo?
Bob
Does that work with ParameterInfo?
Bob
ASKER
No, I already tried that and ParameterInfo has no GetValue member
Right ;) That was my point exactly *GRIN*
Bob
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)
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)
ASKER
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#?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Explanation:
http://groups.google.com/group/microsoft.public.dotnet.framework.clr/browse_frm/thread/c17adcb277967565?hl=en&lr=&ie=UTF-8&oe=UTF-8&rnum=7&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26q%3DStackTrace%2Bclass%2BValue
Bob