Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Reflection: Get Subclass Type [modified]

Reflection: Get Subclass Type [modified]

Scheduled Pinned Locked Moved Visual Basic
questionoop
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KenBonny
    wrote on last edited by
    #1

    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 Class

    Class 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 Base

    EDIT: 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

    L L 2 Replies Last reply
    0
    • K KenBonny

      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 Class

      Class 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 Base

      EDIT: 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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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 Class

      Class SubClass ' "Sub" is a reserved word and hence not a good idea as a classname
      Inherits Base

      ' Some unrelated code
      End Class

      Module 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 Module

      Bastard Programmer from Hell :suss:

      K 1 Reply Last reply
      0
      • K KenBonny

        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 Class

        Class 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 Base

        EDIT: 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

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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

        K 1 Reply Last reply
        0
        • L Lost User

          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 Class

          Class SubClass ' "Sub" is a reserved word and hence not a good idea as a classname
          Inherits Base

          ' Some unrelated code
          End Class

          Module 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 Module

          Bastard Programmer from Hell :suss:

          K Offline
          K Offline
          KenBonny
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • L Luc Pattyn

            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

            K Offline
            K Offline
            KenBonny
            wrote on last edited by
            #5

            Thanks mate, I needed the Object.GetType(), used Me.GetType() works too. Thanks for the help, both of you!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups