Reflection: Get Subclass Type [modified]
-
I have a situation with inheritance. In my base class I have a function GetName() which I want to supply the name of the class that calls the function. But how do I give the name of my current subclass and not the base class? This is my code so far.
Class Base
Public Function GetName() As String
' Bad code
'Return System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name.ToLower()' Good code Dim type as Type = Me.GetType() Return type.Name
End Function
End ClassClass Sub
Inherits Base' Some unrelated code
End Class' In program somewhere
Dim s as Sub
Console.PrintLine(s.GetName()) ' should print out Sub, but it prints BaseEDIT: I found the solution. about 30 minutes after this post, I found it. :( Adjusted the code.
modified on Wednesday, June 8, 2011 9:43 AM
-
I have a situation with inheritance. In my base class I have a function GetName() which I want to supply the name of the class that calls the function. But how do I give the name of my current subclass and not the base class? This is my code so far.
Class Base
Public Function GetName() As String
' Bad code
'Return System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name.ToLower()' Good code Dim type as Type = Me.GetType() Return type.Name
End Function
End ClassClass Sub
Inherits Base' Some unrelated code
End Class' In program somewhere
Dim s as Sub
Console.PrintLine(s.GetName()) ' should print out Sub, but it prints BaseEDIT: I found the solution. about 30 minutes after this post, I found it. :( Adjusted the code.
modified on Wednesday, June 8, 2011 9:43 AM
Hope this helps;
Class Base
Public Function GetClassName() As String
Return System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name.ToLower()
End Function
Public Function GetCallingMethodName() As String
Return New StackTrace().GetFrame(1).GetMethod().Name
End Function
Public Function GetMethodName() As String
Return New StackTrace().GetFrame(0).GetMethod().Name
End Function
End ClassClass SubClass ' "Sub" is a reserved word and hence not a good idea as a classname
Inherits Base' Some unrelated code
End ClassModule Module1
Sub main()
Dim s As New SubClass
Console.WriteLine(s.GetClassName()) ' prints the classname
Console.WriteLine(s.GetCallingMethodName()) ' prints the methodname (main)
Console.WriteLine(s.GetMethodName()) ' prints the methodname that's currently executed
Console.ReadKey()
End Sub
End ModuleBastard Programmer from Hell :suss:
-
I have a situation with inheritance. In my base class I have a function GetName() which I want to supply the name of the class that calls the function. But how do I give the name of my current subclass and not the base class? This is my code so far.
Class Base
Public Function GetName() As String
' Bad code
'Return System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name.ToLower()' Good code Dim type as Type = Me.GetType() Return type.Name
End Function
End ClassClass Sub
Inherits Base' Some unrelated code
End Class' In program somewhere
Dim s as Sub
Console.PrintLine(s.GetName()) ' should print out Sub, but it prints BaseEDIT: I found the solution. about 30 minutes after this post, I found it. :( Adjusted the code.
modified on Wednesday, June 8, 2011 9:43 AM
if you want information about the caller, Environment.StackTrace may be useful. if you want information about the base class of some object, look at Object.GetType(). and then there is reflection. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3 -
Hope this helps;
Class Base
Public Function GetClassName() As String
Return System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name.ToLower()
End Function
Public Function GetCallingMethodName() As String
Return New StackTrace().GetFrame(1).GetMethod().Name
End Function
Public Function GetMethodName() As String
Return New StackTrace().GetFrame(0).GetMethod().Name
End Function
End ClassClass SubClass ' "Sub" is a reserved word and hence not a good idea as a classname
Inherits Base' Some unrelated code
End ClassModule Module1
Sub main()
Dim s As New SubClass
Console.WriteLine(s.GetClassName()) ' prints the classname
Console.WriteLine(s.GetCallingMethodName()) ' prints the methodname (main)
Console.WriteLine(s.GetMethodName()) ' prints the methodname that's currently executed
Console.ReadKey()
End Sub
End ModuleBastard Programmer from Hell :suss:
The Reflection will give you the "Base" name. The StackTrace would give you the name of the calling Mehtod. I needed the name of the type of the subclass. I know Sub is not a valid name, but I was making a quick example program, my actual program is a lot more complex, but I just needed the type of the subclass.
-
if you want information about the caller, Environment.StackTrace may be useful. if you want information about the base class of some object, look at Object.GetType(). and then there is reflection. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3