Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Obtaining Caller of a Method in C#

Obtaining Caller of a Method in C#

Scheduled Pinned Locked Moved C#
csharptutorialquestion
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheLastBastion
    wrote on last edited by
    #1

    I was wondering if there is any (preferably relatively straighforward) way for a method in C# to obtain the object which made the call. For example...

    namespace Example
    {
    public class A
    {

      private B b;
    
      public A()
      {
         b = new B();
         b.Call();
      }
    

    }

    public class B
    {
    public B()
    {
    }

      public void Call()
      {
        //So, who called this function?
        object caller = null;   // Replace null with what so that
                                // we get the instance of A making
                                // the call.
      }
    

    }

    }

    I hope this makes some sense. Any hints / tips are much appreciated. -- The Last Bastion

    L J J 3 Replies Last reply
    0
    • T TheLastBastion

      I was wondering if there is any (preferably relatively straighforward) way for a method in C# to obtain the object which made the call. For example...

      namespace Example
      {
      public class A
      {

        private B b;
      
        public A()
        {
           b = new B();
           b.Call();
        }
      

      }

      public class B
      {
      public B()
      {
      }

        public void Call()
        {
          //So, who called this function?
          object caller = null;   // Replace null with what so that
                                  // we get the instance of A making
                                  // the call.
        }
      

      }

      }

      I hope this makes some sense. Any hints / tips are much appreciated. -- The Last Bastion

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      public void Call(A caller)
      {
      object callerr = caller;
      }

      leppie::AllocCPArticle("Zee blog");

      1 Reply Last reply
      0
      • T TheLastBastion

        I was wondering if there is any (preferably relatively straighforward) way for a method in C# to obtain the object which made the call. For example...

        namespace Example
        {
        public class A
        {

          private B b;
        
          public A()
          {
             b = new B();
             b.Call();
          }
        

        }

        public class B
        {
        public B()
        {
        }

          public void Call()
          {
            //So, who called this function?
            object caller = null;   // Replace null with what so that
                                    // we get the instance of A making
                                    // the call.
          }
        

        }

        }

        I hope this makes some sense. Any hints / tips are much appreciated. -- The Last Bastion

        J Offline
        J Offline
        James Simpson
        wrote on last edited by
        #3

        Have a look at the stack trace objects in the debugging namespace, cant remember offhand where it is (not on a .NET Pc at the moment!) System.Diagnostics might be the one. Not 100% sure if it returns the object, or just a list of methods called. Cheers, James James Simpson Web Developer imebgo@hotmail.com

        L 1 Reply Last reply
        0
        • J James Simpson

          Have a look at the stack trace objects in the debugging namespace, cant remember offhand where it is (not on a .NET Pc at the moment!) System.Diagnostics might be the one. Not 100% sure if it returns the object, or just a list of methods called. Cheers, James James Simpson Web Developer imebgo@hotmail.com

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          I was typing that, then i realized, hey where is the object? :doh: leppie::AllocCPArticle("Zee blog");

          J 1 Reply Last reply
          0
          • L leppie

            I was typing that, then i realized, hey where is the object? :doh: leppie::AllocCPArticle("Zee blog");

            J Offline
            J Offline
            James T Johnson
            wrote on last edited by
            #5

            leppie wrote: hey where is the object? There isn't one, also what would you do if the caller was a static method? In that case there is no object reference that is doing the calling. AFAIK, the best you can hope for is to get the class/struct name and possibly get the Type object representing it. James At Jethro Tull's August 28, 2003 concert Ian Anderson mentioned that the group would be performing a medley of title tracks. The songs were "Songs from the Wood", "Too Old to Rock and Roll; Too Young to Die"; and from the Heavy Horses album, "Stairway to Heaven".

            1 Reply Last reply
            0
            • T TheLastBastion

              I was wondering if there is any (preferably relatively straighforward) way for a method in C# to obtain the object which made the call. For example...

              namespace Example
              {
              public class A
              {

                private B b;
              
                public A()
                {
                   b = new B();
                   b.Call();
                }
              

              }

              public class B
              {
              public B()
              {
              }

                public void Call()
                {
                  //So, who called this function?
                  object caller = null;   // Replace null with what so that
                                          // we get the instance of A making
                                          // the call.
                }
              

              }

              }

              I hope this makes some sense. Any hints / tips are much appreciated. -- The Last Bastion

              J Offline
              J Offline
              Jerry Dennany
              wrote on last edited by
              #6

              My article on custom trace listeners has a decent write-up on this: TraceListeners and Reflection Towards the end there's a section on "StackFrame and StackTrace", with some example code. Jerry Dennany

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups