Find methods in a class generically

Posted on 2007-10-17
Last Modified: 2010-04-23
I have a question relate to Reflection.  I have custom classes that I want to inspect.  However, I don't necessarily know the name of the class until run-time.  For example, the following code works:

Module Module1

    Sub Main()

        Dim ObjectType As Type = GetType(tmpClass)

        For Each mi As Reflection.MethodInfo In ObjectType.GetMethods


    End Sub

    Class tmpClass

        Public Sub Method1()

        End Sub

    End Class

End Module

The problem is in the first line in the Sub Main.  Instead of using
        Dim ObjectType As Type = GetType(tmpClass)
I want to use something like
        Dim ClassName="tmpClass"
        Dim ObjectType As Type = GetType(ClassName)

This would allow me to specify the class name that I want to inspect at run-time.

Is this possible?  How?
Question by:chlade
    LVL 96

    Expert Comment

    by:Bob Learned
    Try this:

            Dim classType As Type = Type.GetType(Me.GetType().Namespace & ".Class1", True, True)

            If Not classType Is Nothing Then
                For Each mi As Reflection.MethodInfo In classType.GetMethods()
            End If


    Author Comment

    That doesn't seem to do it.  I tried it with my sample code and couldn't get it to work.

    However, that code was for a test scenario.  Here's an example that is closer to what I'm trying to do.  It's also more concise.

    If you run the following code, the first statement works but the second one returns Nothing.  How can I get the second statement to return a proper result?

        Dim ObjectType1 As Type = GetType(System.Data.SqlClient.SqlDataReader)
        Dim ObjectType2 As Type = Type.GetType("System.Data.SqlClient.SqlDataReader")
    LVL 96

    Accepted Solution

    1) If you set the FailOnError = True, then you will get an exception:

            Dim ObjectType2 As Type = Type.GetType("System.Data.SqlClient.SqlDataReader", True)

    Could not load type 'System.Data.SqlClient.SqlDataReader' from assembly

    2) Explanation:

    Using Type.GetType(typeName)

    Type.GetType("System.Uri, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

    3) Use Windows Explorer to navigate to the GAC (C:\Windows\Assembly), and view the properties for the System.Data.dll:

            System.Data, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089

    4) Proper syntax for finding a type outside of the current assembly:

            Dim ObjectType2 As Type = Type.GetType("System.Data.SqlClient.SqlDataReader, System.Data, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089")


    Author Comment

    Thanks.  Not the answer I was hoping for, but it does answer the question.  Thanks so much for the excellent help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now