Home All Groups Group Topic Archive Search About

Determine caller class name

Author
31 May 2006 3:28 AM
qingxun
Hi!

Is there a way in VB.NET to determine the caller's class name?

System.Reflection.MethodBase.GetCurrentMethod.GetType basically telling
me what is my current executing class, but I wanted to know, which
caller class invoke my existing class method.

Any help is much appreciated.


Bob

Author
31 May 2006 12:58 PM
Oenone
qing***@gmail.com wrote:
> Is there a way in VB.NET to determine the caller's class name?

\\\
  Dim stack As New StackTrace
  Dim className As String

  className = stack.GetFrame(1).GetMethod.DeclaringType.Name
///

The StackTrace object will contain details about the current call stack. The
first element in the collection (element zero) will be your own current
method, elements with higher indices will be the methods further up the
call-stack. As the code that calls you will always be the second element on
the call stack, you can access it using stack.GetFrame(1).

HTH,

--

(O)enone
Author
6 Jun 2006 2:51 AM
qingxun
Thanks a lot. It definitely helps.

Oenone wrote:
Show quoteHide quote
> qing***@gmail.com wrote:
> > Is there a way in VB.NET to determine the caller's class name?
>
> \\\
>   Dim stack As New StackTrace
>   Dim className As String
>
>   className = stack.GetFrame(1).GetMethod.DeclaringType.Name
> ///
>
> The StackTrace object will contain details about the current call stack. The
> first element in the collection (element zero) will be your own current
> method, elements with higher indices will be the methods further up the
> call-stack. As the code that calls you will always be the second element on
> the call stack, you can access it using stack.GetFrame(1).
>
> HTH,
>
> --
>
> (O)enone