locked
Getting the name of the inherited class of a function with reflection, staticly RRS feed

  • Question

  • User-106545718 posted

    Hi,

    My problem is not as simple as it looks like. Let's check the code here :

    1        Private Class ClassNameWanted
    2            Inherits BaseClass
    3    
    4        End Class
    5    
    6        Private Class BaseClass
    7    
    8            Public Shared Function AnyMethod() As String
    9    
    10               Return WHAT ????
    11   
    12           End Function
    13   
    14       End Class
    As you can see, the method is static. I have to get the name of the class "ClassNameWanted" within the function "AnyMethod" in the base class.
    Everything I do always give me "BaseClass", not "ClassNameWanted".
    I know it can be done, it HAS to be possible, but...
    Monday, April 7, 2008 7:12 PM

Answers

  • User-647234374 posted

    Why would it have to be possible? It's static. You have to invoke it using a class to begin with. Pass a type if you need it.

    Imports System.Type
    Imports System.IO
    Public Class TestClass
        Inherits SuperClass

        Public MyData As Integer

    End Class

    Public Class SuperClass
        Public Shared Function SharedMethod(ByVal T As Type) As String
            Return T.ToString
        End Function
    End Class

    TestClass.SharedMethod(GetType(TestClass)) will output TestClass 

    Seems a bit redundant, though. I wonder why you need it; perhaps you should be using an interface instead? This kind of sounds like factory methods, but those would override as required. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 7, 2008 10:38 PM

All replies

  • User-647234374 posted

    Why would it have to be possible? It's static. You have to invoke it using a class to begin with. Pass a type if you need it.

    Imports System.Type
    Imports System.IO
    Public Class TestClass
        Inherits SuperClass

        Public MyData As Integer

    End Class

    Public Class SuperClass
        Public Shared Function SharedMethod(ByVal T As Type) As String
            Return T.ToString
        End Function
    End Class

    TestClass.SharedMethod(GetType(TestClass)) will output TestClass 

    Seems a bit redundant, though. I wonder why you need it; perhaps you should be using an interface instead? This kind of sounds like factory methods, but those would override as required. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 7, 2008 10:38 PM
  • User-106545718 posted

    Thanks,

    Your answer is what I decided to do last night to solve my problem. What I don't understand is why the hell is it different because it is static ??

    The problem I got is I have a business layer where everything is static. I have many classes like "SupportRequestManager", "MemberManager", "ForumCategoriesManager", "ForumSubjectsManager" who has methods like "Add", "Delete" and "Modify". Every "manager" has a base class "ManagerBase" that has a function to get labels from an xml file name after the derived manager class, like "SupportRequestManager.xml". I have the same thing that works for my aspx pages. I have a base class for everypage and from the base class I can get the name of the derived type to get the xml file.

    Tuesday, April 8, 2008 12:19 PM
  • User-647234374 posted

    Static methods don't require an instance of a class, so there is no real way to get the type of the class that invoked them, since there isn't one.

    I would not design the classes so interdependently, as it might bite me later. I'd have an abstract method called GetXml() in the base class that returned an XmlDocument. All child classes would override it to get their data as appropriate. That XmlDocument would be passed to the static method when it gets invoked. Sure, it's more work at first, but most of it would be copy/paste and it would likely save me a headache (and hack or redesign) later.

    Tuesday, April 8, 2008 1:14 PM