Discovering calling method name and its parameters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • juchytil

    Discovering calling method name and its parameters

    Here is a method that calls a static GetInfo() method on the MyObject class.

    public void MethodName(stri ng param1, int param2)
    {
    MyObject.GetInf o();
    }

    Is there any way for the GetInfo() method to find out the name and
    parameters of the method it was called from?

    For example

    public static void GetInfo()
    {
    // Is there some query I can make that would return the name 'MethodName'
    // and the names of its parameters 'param1' and 'param2' and their values?
    }

    Can this be done with System.Reflecti on?

    Joe
  • Patrice

    #2
    Re: Discovering calling method name and its parameters

    See the StackFrame class. You can have the name of the method. Likely its
    parameters (you have this info anyway in reflection). Not sure though about
    the values.

    Please let us know about your findings...

    Patrice

    --

    "juchytil" <juchytil@discu ssions.microsof t.com> a écrit dans le message de
    news:DC66355B-B66B-473C-A680-808F68649116@mi crosoft.com...[color=blue]
    > Here is a method that calls a static GetInfo() method on the MyObject[/color]
    class.[color=blue]
    >
    > public void MethodName(stri ng param1, int param2)
    > {
    > MyObject.GetInf o();
    > }
    >
    > Is there any way for the GetInfo() method to find out the name and
    > parameters of the method it was called from?
    >
    > For example
    >
    > public static void GetInfo()
    > {
    > // Is there some query I can make that would return the name[/color]
    'MethodName'[color=blue]
    > // and the names of its parameters 'param1' and 'param2' and their[/color]
    values?[color=blue]
    > }
    >
    > Can this be done with System.Reflecti on?
    >
    > Joe[/color]


    Comment

    • Imran Koradia

      #3
      Re: Discovering calling method name and its parameters

      Joe,

      As Patrice mentioned, you can get the method information using the Stackframe
      class. Here's how you would do it:

      Imports System.Reflecti on
      Imports System.Diagnost ics

      Private Sub Button1_Click(B yVal sender As System.Object, _
      ByVal e As System.EventArg s) Handles Button1.Click
      TestStackFrame( )
      End Sub

      Private Sub TestStackFrame( )
      ' 0 means start from current stack frame
      Dim oTrace As New StackTrace(0, True)
      ' 0 means the current stack frame, 1 means previous, etc
      Dim oMethod As MethodBase = oTrace.GetFrame (1).GetMethod
      MessageBox.Show (oMethod.Name)
      For Each oParam As ParameterInfo In oMethod.GetPara meters()
      MessageBox.Show (oParam.Name & " - " & oParam.Paramete rType.Name)
      Next
      End Sub

      The above code will print out Button1_Click and its parameters.

      However!
      I'm not sure there's an easy way to get the parameter values. You surely
      cannot get the values using either reflection or the Stack classes. You might
      be able to use debugger APIs to get the values but that might be a little
      more complicated. I haven't seen anyone do this before so I would surely
      love to know if you find a way to get the method parameter values.


      hope that helps..
      Imran.


      [color=blue]
      > Here is a method that calls a static GetInfo() method on the MyObject
      > class.
      >
      > public void MethodName(stri ng param1, int param2)
      > {
      > MyObject.GetInf o();
      > }
      > Is there any way for the GetInfo() method to find out the name and
      > parameters of the method it was called from?
      >
      > For example
      >
      > public static void GetInfo()
      > {
      > // Is there some query I can make that would return the name
      > 'MethodName'
      > // and the names of its parameters 'param1' and 'param2' and their
      > values?
      > }
      > Can this be done with System.Reflecti on?
      >
      > Joe
      >[/color]



      Comment

      Working...