I will like to have a generic form to test my forms. The idea is to show a Listbox with the public types defined in the application, display them in a ListBox, and let me select from the listbox what form to run. I want to use this generic form on any project, so I will not have to "wire" it.
This is a non working sample: the myType.InvokeMethod statement is incorrect. I need to create the object first. I need a sort of Application.Run() on the button1_click, however, not sure how to create an object using reflection to use it.
Question are:
using System.Reflection,
How do you "create" an object knowing it's "Type"
What method is invoked to "run" the previously created object.
<PRE>
' Requires ListBox1 and a Button1
' Using a support class to store Type and display just the "Name" in the listbox
'
Dim myAssembly As System.Reflection.Assembly
Private Class xMod
Public Type As Type
Public Sub New(ByVal Type As Type)
Me.Type = Type
End Sub
Public Overrides Function ToString() As String
Return Type.Name
End Function
End Class
Private Sub Runner_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myAssembly = System.Reflection.Assembly
.GetEntryA
ssembly()
For Each [MyModule] As System.Reflection.Module In myAssembly.GetModules(Fals
e)
Debug.WriteLine(MyModule.F
ullyQualif
iedName) ' may be just one...
For Each myType As System.Type In MyModule.GetTypes()
ListBox1.Items.Add(New xMod(myType))
Next
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim app As xMod
app = CType(ListBox1.SelectedIte
m, xMod)
Dim myType As Type = app.Type
myType.InvokeMember("Show"
, _
Reflection.BindingFlags.De
fault Or _
Reflection.BindingFlags.In
vokeMethod
Or _
System.Reflection.BindingF
lags.Stati
c, _
Nothing, Nothing, New Object() {})
End Sub
</PRE>
Start Free Trial