Avatar of David Bach
David BachFlag for United States of America

asked on 

Reference To Name Of Calling Class From Within The Called Class ASP.NET/VB.NET

Greetngs:

I have a Visual Studio Pro 2015 solution containing 1 web project and multiple class libraries all written in VB.NET.

A web page code behind calls class 2 in class library A. Class 2 calls class 5 in class library B.

I need to know the name of class 2, the calling class, from within class 5, the called class.

Is there a way to reference class 2's name?


Much thanks,
David Bach
ASP.NETVisual Basic.NET

Avatar of undefined
Last Comment
David Bach
Avatar of Ark
Ark
Flag of Russian Federation image

Dim st As New StackTrace
Debug.WriteLine("Calling class name: " & st.GetFrame(1).GetMethod.ReflectedType.Name)

Open in new window

Avatar of David Bach
David Bach
Flag of United States of America image

ASKER

Hello Ark:

Thank you for your quick response.

I tried your code snippet, however, I saw the name of the class currently executing and not the name of the calling class.


David
Avatar of Ark
Ark
Flag of Russian Federation image

Put this code into child class initializing subroutine (or any method, calling by parent class). Note that GetFrame(1) get first parent method that call child class. You can see stack trace when you get an exception. To enumerate all calls:
Dim strace As New StackTrace(0)
Dim stFrames As StackFrame() = strace.GetFrames()

Dim sf As StackFrame
For Each sf In stFrames
      Debug.WriteLine("Method: {0}", sf.GetMethod())
Next sf

Open in new window

See MSDN for details.
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of kaufmed
kaufmed
Flag of United States of America image

If you need the class name and you follow the one class per file convention, then you could use the CallerFilePathAttribute to get the filename and then just strip off the path and extension.

If you needed the caller's method name, then you could use the CallerMemberNameAttribute.
Avatar of David Bach
David Bach
Flag of United States of America image

ASKER

Greetings Ark:

I apologize for the delay in closing this question. Your solution is definitely something I can work with.


Much thanks,
David Bach
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo