How to get name of 'Caller' class from 'Called' class (ASP.NET)

Discussion in 'ASP .Net' started by =?Utf-8?B?SG1udA==?=, Apr 28, 2004.

  1. Hi
    Hope ur smart enough to get what i wanna ask ... (from the sub line

    Brief Explaination
    I've 2 classas Class1 and Class
    Now i've an instance of Class2 ( Class2 objC2 = new Class2();
    Next i access a method of Class2 (say objC2.func1();
    Now in function 'func1()' of Class2 I wanna get the Caller class name ('Class1'

    Plz don say me to pass the name of class as an arg in 'func()'; i need som other way ... this is coz Class2 is a central class which is used by many other classes !!

    All the Best ...
     
    =?Utf-8?B?SG1udA==?=, Apr 28, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?SG1udA==?=

    CT Guest

    This might be worth a look:
    http://msdn.microsoft.com/library/d...fsystemreflectionmemberinfoclassnametopic.asp

    --
    Carsten Thomsen
    Enterprise Development with VS .NET, UML, and MSF
    http://www.apress.com/book/bookDisplay.html?bID=105
    "Hmnt" <> wrote in message
    news:...
    > Hi,
    > Hope ur smart enough to get what i wanna ask ... (from the sub line)
    >
    > Brief Explaination:
    > I've 2 classas Class1 and Class2
    > Now i've an instance of Class2 ( Class2 objC2 = new Class2();)
    > Next i access a method of Class2 (say objC2.func1();)
    > Now in function 'func1()' of Class2 I wanna get the Caller class name
    > ('Class1')
    >
    > Plz don say me to pass the name of class as an arg in 'func()'; i need som
    > other way ... this is coz Class2 is a central class which is used by many
    > other classes !!!
    >
    > All the Best ...
     
    CT, Apr 28, 2004
    #2
    1. Advertising

  3. This sounds like an extremely bad idea. What is Class2.func1 planning to do
    with the name of its caller class, and why can't you just pass it the name
    of the class as a string?

    I can't recall the last time I've seen a method which needed to know who
    called it. Maybe back in the early 80's...
    --
    John Saunders
    John.Saunders at SurfControl.com


    "Hmnt" <> wrote in message
    news:...
    > Hi,
    > Hope ur smart enough to get what i wanna ask ... (from the sub line)
    >
    > Brief Explaination:
    > I've 2 classas Class1 and Class2
    > Now i've an instance of Class2 ( Class2 objC2 = new Class2();)
    > Next i access a method of Class2 (say objC2.func1();)
    > Now in function 'func1()' of Class2 I wanna get the Caller class name

    ('Class1')
    >
    > Plz don say me to pass the name of class as an arg in 'func()'; i need som

    other way ... this is coz Class2 is a central class which is used by many
    other classes !!!
    >
    > All the Best ...
     
    John Saunders, Apr 28, 2004
    #3
  4. Well
    U leave me no choice but to explain the details-
    Here I've to maintain a 'Log' of the classes which used the functionality of this
    central-class(actually its my DB-layer file - so i need to track which class executed which
    DB -object-[Stored Proc, INS/UPD/DEL...etc.

    Its really pathetic to send the caller name as a string argument (and even i can't change ALL the functions already implemented !!!

    Hope u understand my prob n fin me a relevent solution ...
     
    =?Utf-8?B?SG1udA==?=, Apr 29, 2004
    #4
  5. If anyone ever looks at your log, except for debugging, I'll be surprised.
    I'll be even more surprised if they get any value out of it.

    The exact calling sequence down to your DB layer (please stop saying "file")
    is likely to change over the course of time. Many of the changes will not be
    changing the semantic reason why your DB layer was called. I hope that's
    what you mean to capture by capturing the name of the calling class. BTW, I
    hope you intended to capture the name of the calling method as well.

    Example: today, my Employee class has a method UpdateHoursWorked which calls
    the DB layer to write the updated hours to the database. Tomorrow I may
    perform the exact same function by having a Timeclock class update the hours
    to the database. There is no semantic difference between these two, yet
    you'll be capturing different information. Good luck in making any use of
    that.

    You'd be better off changing all of the classes which call your DB layer to
    somehow provide the DB layer with the semantics of what they are doing. The
    DB layer could then log that. This could be done via a separate parameter,
    or via CallContext, but the callers shouldn't care about that. Just add a
    static SetSemantics method to your DB layer and let it decide how to store
    the information.

    --
    John Saunders
    John.Saunders at SurfControl.com


    "Hmnt" <> wrote in message
    news:D...
    > Well,
    > U leave me no choice but to explain the details--
    > Here I've to maintain a 'Log' of the classes which used the functionality

    of this
    > central-class(actually its my DB-layer file - so i need to track which

    class executed which
    > DB -object-[Stored Proc, INS/UPD/DEL...etc.]
    >
    > Its really pathetic to send the caller name as a string argument (and even

    i can't change ALL the functions already implemented !!!)
    >
    > Hope u understand my prob n fin me a relevent solution ...
     
    John Saunders, Apr 30, 2004
    #5
  6. =?Utf-8?B?SG1udA==?=

    IcingDeath

    Joined:
    May 23, 2006
    Messages:
    2
    Private Function GetCaller() As String
    Dim mStack As New StackTrace(1, False)
    Return mStack.GetFrame(1).GetType.FullName & "." & mStack.GetFrame(1).GetMethod.Name
    End Function
     
    IcingDeath, Jun 25, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Koen Van Herck

    Retrieve class name of caller

    Koen Van Herck, Sep 19, 2003, in forum: Python
    Replies:
    2
    Views:
    300
    Koen Van Herck
    Sep 22, 2003
  2. Wei Li
    Replies:
    6
    Views:
    822
    Wei Li
    Aug 23, 2004
  3. Mark
    Replies:
    2
    Views:
    407
  4. Thomas Mlynarczyk

    Function.caller if not called from within a function

    Thomas Mlynarczyk, Feb 26, 2005, in forum: Javascript
    Replies:
    4
    Views:
    203
    Thomas Mlynarczyk
    Feb 26, 2005
  5. Replies:
    1
    Views:
    93
    Julian Turner
    Oct 19, 2006
Loading...

Share This Page