Activator.CreateInstance Problems

  • Thread starter Thread starter Huw
  • Start date Start date
H

Huw

I have written a small procedure that opens a dialog that
I pass it.

Sub MyOpen(txtForm as string)

Dim frm As Object
Dim frmType As Type
frmType = Type.GetType(txtForm)
frm = Activator.CreateInstance(frmType)
frmType.InvokeMember("ShowDialog",
BindingFlags.InvokeMethod, Nothing, frm, Nothing)

End Sub

This works fine when I pass it form names that exist in
the current .exe file e.g. MyOpen("MyApp.MyForm")

However what I really want to do is open forms that are
contained in a sperate windows .dll file i.e. MyOpen
("MyDll.MyForm"), but it does not seem to work... Will it
work if I supply the full public token id as the string?
If so how do I obtain the full public token id of a form
in a seperate .dll file?

Thanks in advance
 
Use the static method Assembly.Load(assemblyDisplayName) to load the
assembly that contains the form. Using that assembly reference, make a call
to GetType().
Sub MyOpen(txtForm as string)

--->
Dim asmbly = Assembly.Load("AssemblyDisplayName")
frmType = asmbly.GetType(txtForm)
--->

End Sub

Thanks,
Vijaya Krishna P.
 
Huw,
In addition to Vijaya Krishna P's comments.

You can include the assemble name in the string you pass to Type.GetType.

MyOpen("MyDll.MyForm, MyDll")

Which presumes that you named the assembly & namespace where the MyForm is
the same name (which is the default).

Hope this helps
Jay
 
Back
Top