Reflection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    #1

    Reflection

    Hi,


    How can I get the type of the caller of a method via reflection ? I want
    to know what type actually called the method.

    For example.

    Class A method Afn() calls Class B method Bfn()

    How can I actually programatically via reflection inside Bfn() get the
    C.Afn() type that actually called this?


  • Jon Skeet [C# MVP]

    #2
    Re: Reflection

    <discussion@dis cussion.microso ft.com> wrote:[color=blue]
    > How can I get the type of the caller of a method via reflection ? I want
    > to know what type actually called the method.
    >
    > For example.
    >
    > Class A method Afn() calls Class B method Bfn()
    >
    > How can I actually programatically via reflection inside Bfn() get the
    > C.Afn() type that actually called this?[/color]

    Do you mean the instance of A? You can't. If you need it, make it a
    parameter in the method.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Guest's Avatar

      #3
      Re: Reflection

      I can, I used the StackFrame :D

      System.Diagnost ics;

      StackTrace, StackFrame and MethodBase you can get the info.

      Obviously its the 2nd item on teh call stack that called the method.

      I was hoping there was a nicer way but this way works and as its a debug
      option its not too critical.

      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
      news:MPG.1a1d97 927dfcc142989a9 8@msnews.micros oft.com...[color=blue]
      > <discussion@dis cussion.microso ft.com> wrote:[color=green]
      > > How can I get the type of the caller of a method via reflection ? I[/color][/color]
      want[color=blue][color=green]
      > > to know what type actually called the method.
      > >
      > > For example.
      > >
      > > Class A method Afn() calls Class B method Bfn()
      > >
      > > How can I actually programatically via reflection inside Bfn() get the
      > > C.Afn() type that actually called this?[/color]
      >
      > Do you mean the instance of A? You can't. If you need it, make it a
      > parameter in the method.
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Reflection

        <discussion@dis cussion.microso ft.com> wrote:[color=blue]
        > I can, I used the StackFrame :D
        >
        > System.Diagnost ics;
        >
        > StackTrace, StackFrame and MethodBase you can get the info.[/color]

        No it can't. It can get the *type*, but not the *instance*, which is
        why I asked whether or not you meant the instance.
        [color=blue]
        > Obviously its the 2nd item on teh call stack that called the method.
        >
        > I was hoping there was a nicer way but this way works and as its a debug
        > option its not too critical.[/color]

        Don't forget that it may not be accurate (due to inlining etc).

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Guest's Avatar

          #5
          Re: Reflection

          I dont want the actual instance, just where it was called.

          As for inlining, well at lesat I will have the ReflectedType from
          MethodBase, so its better than nothing.



          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          news:MPG.1a1d9e 1fce0f317f989a9 b@msnews.micros oft.com...[color=blue]
          > <discussion@dis cussion.microso ft.com> wrote:[color=green]
          > > I can, I used the StackFrame :D
          > >
          > > System.Diagnost ics;
          > >
          > > StackTrace, StackFrame and MethodBase you can get the info.[/color]
          >
          > No it can't. It can get the *type*, but not the *instance*, which is
          > why I asked whether or not you meant the instance.
          >[color=green]
          > > Obviously its the 2nd item on teh call stack that called the method.
          > >
          > > I was hoping there was a nicer way but this way works and as its a debug
          > > option its not too critical.[/color]
          >
          > Don't forget that it may not be accurate (due to inlining etc).
          >
          > --
          > Jon Skeet - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet
          > If replying to the group, please do not mail me too[/color]


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Reflection

            <discussion@dis cussion.microso ft.com> wrote:[color=blue]
            > I dont want the actual instance, just where it was called.[/color]

            Right.
            [color=blue]
            > As for inlining, well at lesat I will have the ReflectedType from
            > MethodBase, so its better than nothing.[/color]

            My point is that you might have missed a few stack frames. For
            instance, the stack could theoretically be:

            Foo.SomeMethod
            Bar.SomeInlined Method
            Gronk.AnotherIn linedMethod
            Baz.SomeBigMeth od

            and the stack that you get back in the stack trace could be:

            Foo.SomeMethod
            Baz.SomeBigMeth od

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Guest's Avatar

              #7
              Re: Reflection

              Thtats why optimization = off for debug builds, right.

              Is there anyway to get where this was called in optimized builds (with
              inlining), i just need the actual namespace, class and method that actually
              called this (according to my actual code).





              "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
              news:MPG.1a1da4 383f1259e7989a9 c@msnews.micros oft.com...[color=blue]
              > <discussion@dis cussion.microso ft.com> wrote:[color=green]
              > > I dont want the actual instance, just where it was called.[/color]
              >
              > Right.
              >[color=green]
              > > As for inlining, well at lesat I will have the ReflectedType from
              > > MethodBase, so its better than nothing.[/color]
              >
              > My point is that you might have missed a few stack frames. For
              > instance, the stack could theoretically be:
              >
              > Foo.SomeMethod
              > Bar.SomeInlined Method
              > Gronk.AnotherIn linedMethod
              > Baz.SomeBigMeth od
              >
              > and the stack that you get back in the stack trace could be:
              >
              > Foo.SomeMethod
              > Baz.SomeBigMeth od
              >
              > --
              > Jon Skeet - <skeet@pobox.co m>
              > http://www.pobox.com/~skeet
              > If replying to the group, please do not mail me too[/color]


              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Reflection

                <discussion@dis cussion.microso ft.com> wrote:[color=blue]
                > Thtats why optimization = off for debug builds, right.
                >
                > Is there anyway to get where this was called in optimized builds (with
                > inlining), i just need the actual namespace, class and method that actually
                > called this (according to my actual code).[/color]

                I don't believe so. In practice, I think it will *often* get it right,
                but it's not guaranteed.

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                Working...