What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleList and create instances of the shape types defined in an assembly in VB .NET
DescriptionThis example shows how to list and create instances of the shape types defined in an assembly in VB .NET.
Keywordsassembly, type, data type, reflection, invoke, shapes, VB .NET
This example shows how to learn about the types defined in an assembly and create instances of them at run time. In this example, the types are classes representing shapes (Ellipse and Rectangle).

Before you run the main program, load the project ShapeClasses and compile it. This project creates a DLL that defines the Ellipse and Rectangle classes. These classes have constructors similar to this one:

Public Sub New(ByVal x As Integer, ByVal y As Integer, _
    ByVal wid As Integer, ByVal hgt As Integer, ByVal _
    back_color As Color, ByVal fore_color As Color)
    m_X = x
    m_Y = y
    m_Width = wid
    m_Height = hgt
    m_BackColor = back_color
    m_ForeColor = fore_color
End Sub
After you build the DLL, run the main program. When the program starts, the Form_Load event handler locates the DLL (make sure you keep the two projects in the same directory) and makes an Assembly object representing it. Note that the code must include the type Assembly in square brackets because "Assembly" is also a keyword.

The program loops through the values returned by the Assembly's GetTypes method, listing the types.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ' Find the shapes assembly.
    Dim shape_dll_path As String = Application.StartupPath
    shape_dll_path = shape_dll_path.Substring(0, _
    shape_dll_path = shape_dll_path.Substring(0, _
    shape_dll_path &= "\ShapeClasses\bin\ShapeClasses.dll"

    ' Get the assembly.
    Dim assem As [Assembly] = _

    ' Make a list of available shapes.
    For Each defined_type As System.Type In assem.GetTypes()
    Next defined_type
    cboShape.SelectedIndex = 1
End Sub
Select the Ellipse or Rectangle type from the list. Enter parameters in the main program to define the new object's location, size, and colors. Then click the Create button. The program retrieves the System.Type for the selected shape. It then builds an array of Type objects giving the kinds of parameters that the desired Rectangle/Ellipse constructor will take. It then uses that array and the Type object's GetConstrucvtor method to get a ConstructorInfo object describing the desired constructor.

Next the program makes an array containing the parameter values it needs to pass to the constructor and calls the ConstructorInfo's Invoke method, passing it the parameters. The program adds the resulting object (a Rectangle or Ellipse) to its m_Objects collection and invalidates its drawing surface.

Private Sub btnCreate_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnCreate.Click
    ' Create a new object of the selected type.
    If cboShape.SelectedItem Is Nothing Then Exit Sub

    ' Get info for an empty constructor for the type.
    Dim selected_type As System.Type = _
    Dim constructor_info As ConstructorInfo
    Dim types() As System.Type = { _
        GetType(Integer), _
        GetType(Integer), _
        GetType(Integer), _
        GetType(Integer), _
        GetType(Color), _
        GetType(Color) _
    constructor_info = selected_type.GetConstructor(types)

    ' Invoke the constructor.
    Dim params() As System.Object = { _
        Integer.Parse(txtX.Text), _
        Integer.Parse(txtY.Text), _
        Integer.Parse(txtWidth.Text), _
        Integer.Parse(txtHeight.Text), _
        picFill.BackColor, _
        picBorder.BackColor _
    Dim obj As Object = constructor_info.Invoke(params)


End Sub
When the program's drawing surface needs painting, the program invokes its object's Draw methods. That method takes a Graphics object as a parameter.

To do this, the program makes an array containing the types of the parameter list: the Graphics type. It then loops through the Rectangles and Ellipses in its m_Objects collection. For each object, it gets the object's Type. It uses the Type's GetMethod function to get a MethodInfo object representing the Draw method that takes a Graphics object as a parameter. The program then calls the MethodInfo's Invoke method, passing it the drawing surface.

Private Sub picCanvas_Paint(ByVal sender As Object, ByVal e _
    As System.Windows.Forms.PaintEventArgs) Handles _
    Dim types() As System.Type = _
    For Each obj As Object In m_Objects
        ' Get information on this object's Draw method.
        Dim obj_type As System.Type = obj.GetType()
        Dim method_info As MethodInfo = _
            obj_type.GetMethod("Draw", types)

        ' Invoke the Draw method.
        If Not (method_info Is Nothing) Then
            Dim params() As System.Object = {e.Graphics}
            method_info.Invoke(obj, params)
        End If
    Next obj
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.