Identifying the method and class name programmatically

L

lisa

Is there any way to tell, programmatically, what method and class
you're in? For example, if I'm doing error handling, I don't want to
have to type the name of the method and class each time; I'd rather
have a stock thing I can just paste in everywhere.

Thanks,
Lisa
 
C

Cowboy \(Gregory A. Beamer\)

A certain amount of this information is automatically processed and added to
the exception objects. Look, for example, at exceptionObject.StatckTrace,
which contains the error point, as well as the entire stack trace a called b
which called c which called d.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

*************************************************
Think outside of the box!
*************************************************
 
N

Nicolas Guinet

A a = new A();
a.Function_1(0);


class A
{
public A() { }
public void Function_1(int d)
{
try
{
double c = 5/d;
}
catch (Exception e)
{
MessageBox.Show( " ERROR IN CLASS '" + this.GetType().Name +
"' ON FUNCTION '" + e.TargetSite + "'");
}

}
}

Regards

Nicolas Guinet
 
L

lisa

Thank you so much!

Lisa



Nicolas said:
A a = new A();
a.Function_1(0);


class A
{
public A() { }
public void Function_1(int d)
{
try
{
double c = 5/d;
}
catch (Exception e)
{
MessageBox.Show( " ERROR IN CLASS '" + this.GetType().Name +
"' ON FUNCTION '" + e.TargetSite + "'");
}

}
}

Regards

Nicolas Guinet
 
K

Keith Patrick

For current method without having an exception, try:
new StackTrace().GetFrame(0).GetMethod()

Be forwarned - reflection-based actions like this are computationally
expensive, so use sparingly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top