Is there a way to get the instance's class name with VB.NET?
Asked
Active
Viewed 4.0k times
4 Answers
22
Try the following
Dim name = Me.GetType().Name
Or for any instance
Dim name = theObject.GetType().Name

JaredPar
- 733,204
- 149
- 1,241
- 1,454
-
how would you do this at the shared level? supporting subclasses as well? – NullVoxPopuli Mar 03 '15 at 20:38
1
This could be better when you are using asp.net website class not object.
Dim ClassName as string = Me.GetType().BaseType.FullName
OR
when you are using desktop app.
Dim ClassName as string = Me.GetType().Name

Rajan
- 303
- 1
- 12
-
1Me.GetType().BaseType.FullName doesn't get me the class name (for a class I coded). It simply returns "System.Object". But me.GetType().Name does return the name of my class. – Dan H. Jun 25 '18 at 14:45
1
Using Reflection, you could do the following...
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString()
I use the following when logging trace info. This gives you the Class name, with all Namespaces and current Method name.
$"{System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString()}.{MethodBase.GetCurrentMethod().Name}"

Chase
- 162
- 1
- 10