Wanting to get some feedback on reflection, if I am doing things in a best practices way. My goal is to let a user create a new project, create forms or usercontrols, and load them dynamically at runtime. One of my concerns is loading the object as an object, and then determining what type it is, a form or a usercontrol. Seems to work fast on my local machine, but if hundreds of these are being loaded, will it be a robust way to do this?
Public Enum DynamicObjectType
Form
UserControl
End Enum
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim DOT As New DynamicObjectType
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
'Provide the current application domain evidence for the assembly.
Dim asEvidence As Evidence = currentDomain.Evidence
'If we don't do this, it won't be able to find the assembly of our custom .dll library
'Load the assembly from the application directory using a simple name.
'Create the assembly
currentDomain.Load("AnotherBigOne", asEvidence)
'Get the form
Dim daForm As Object = CreateObject("AnotherBigOne", "AnotherBigOne.Form1")
If TypeOf daForm Is Form Then
DOT = DynamicObjectType.Form
ElseIf TypeOf daForm Is UserControl Then
DOT = DynamicObjectType.UserControl
End If
For Each ctl As Control In daForm.Controls
If ctl.Name = "TextBox1" Then
DirectCast(ctl, VaultControls.TextBox).Text = DirectCast(ctl, VaultControls.TextBox).Calculation
End If
Next
Select Case DOT
Case DynamicObjectType.Form
daForm.ShowDialog()
Case DynamicObjectType.UserControl
Me.Controls.Add(daForm)
daForm.Parent = PanelMain
daForm.Dock = DockStyle.Fill
daForm.BringToFront()
End Select
End Sub
Private Function CreateObject(ByVal AssemblyName As String, ByVal TypeName As String) As Object
'Make an array for the list of assemblies.
Dim assems As [Assembly]() = AppDomain.CurrentDomain.GetAssemblies()
'Search for the assembly and return the object specified
For Each asm As System.Reflection.Assembly In assems
Debug.WriteLine(asm.FullName)
If asm.FullName.StartsWith(AssemblyName & ",") Then
Return asm.CreateInstance(TypeName)
End If
Next asm
End Function