[RESOLVED] Load a user control onto form using reflection
Hi All,
About 2 years ago I wrote an application that loaded a bunch of user controls onto a form using reflection but being the typical dik I can't find the bleeding code.
I have an assembly "myassembly.dll" which contains a control called "mycontrol".
I have dropped this into the bin of my application which contains a form and a panel. I jsut wnat to load the control into the panel !!
Can someone pint me in the right direction ?
I have to stop throwing my code away :-(
Re: Load a user control onto form using reflection
Was it something like this?
VB.NET Code:
Dim dllAssembly As Reflection.Assembly = Reflection.Assembly.LoadFile("C:\ATI\uc.dll")
Dim ctrl As Control = CType(dllAssembly.CreateInstance(String.Format("{0}.{1}", dllAssembly.GetName().Name, "UserControl1")), Control)
ctrl.Location = New Point(10, 10)
Me.Controls.Add(ctrl)
Re: Load a user control onto form using reflection
Cool, but ctrl always returns NULL.
Re: Load a user control onto form using reflection
Ah yes I forgot to mention, you'd need to change "UserControl1" to hold whatever is the name of your usercontrol, incase you havent already done that :)
Re: Load a user control onto form using reflection
Yeah, I did dio that but still no joy. It must be something I'm doing.
My head is full of messy thinking today.
Re: Load a user control onto form using reflection
Well...CreateInstance takes a string argument that needs to be the full name of the type you want to create, if its not returning anything, you might want to check that the correct string is passed to it.
Run this:
VB.NET Code:
Dim dllAssembly As Reflection.Assembly = Reflection.Assembly.LoadFile("YourDLL.dll")
Dim fullName As String = String.Format("{0}.{1}", dllAssembly.GetName().Name, "YourControl")
MessageBox.Show(fullName)
What did it show you?
Now, have a look at the types that your DLL's assembly exposes by doing:
VB.NET Code:
For Each t As Type In dllAssembly.GetExportedTypes()
MessageBox.Show(t.FullName)
Next
Do you find your UserControl among those exported types? What was its fullname?
Re: Load a user control onto form using reflection
My mistake.
I had my control in a sub folder called Controls meaning I should have called my control "Controls.MyControl".
Thanks.
My stupidity is yet again to blaim.