Identifying the method and class name programmatically

  • Thread starter Thread starter lisa
  • Start date Start date
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
 
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!
*************************************************
 
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
 
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
 
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.
 
Back
Top