Home All Groups Group Topic Archive Search About

Identifying the method and class name programmatically

Author
5 Oct 2006 1:45 PM
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

Author
5 Oct 2006 2:01 PM
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!
*************************************************
<l***@starways.net> wrote in message
Show quote
news:1160055928.399442.73850@m7g2000cwm.googlegroups.com...
> 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
>
Author
5 Oct 2006 2:21 PM
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***@starways.net> a écrit dans le message de news:
1160055928.399442.73***@m7g2000cwm.googlegroups.com...
Show quote
> 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
>
Author
5 Oct 2006 4:37 PM
lisa
Thank you so much!

Lisa



Nicolas Guinet wrote:
Show quote
> 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***@starways.net> a écrit dans le message de news:
> 1160055928.399442.73***@m7g2000cwm.googlegroups.com...
> > 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
> >
Author
5 Oct 2006 7:27 PM
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.

AddThis Social Bookmark Button