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

G

Guest

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 ...
 
J

John Saunders

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 said:
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 !!!
 
G

Guest

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 ...
 
J

John Saunders

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 said:
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 !!!)
 
Joined
May 23, 2006
Messages
2
Reaction score
0
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top