Showing a Windowform with Activator.CreateInstance() - How to ?

  • Thread starter Thread starter Antonio Paglia
  • Start date Start date
A

Antonio Paglia

Hi ,

There is a way to create an instance of windowsforms (and open it) that
is located in another assembly ?

I have tried:

Dim o As Object = System.Activator.CreateInstance("General.Windows.UI.dll",
"MyForm")
CType(o, System.Windows.Forms.Form).Show()

AssemblyName.dll exist in the Bin directory of Caller Assembly but without
reference to it.

I get allways a FileNotFoundException

Any help will be appreciated

TIA
Antonio
 
Antonio said:
There is a way to create an instance of windowsforms (and open it) that is located in another assembly ?

I have tried:

Dim o As Object =
System.Activator.CreateInstance("General.Windows.UI.dll", "MyForm")
CType(o, System.Windows.Forms.Form).Show()

AssemblyName.dll exist in the Bin directory of Caller Assembly but without
reference to it.

I get allways a FileNotFoundException

I assume you mean to say that General.Windows.UI.dll exists in the Bin
directory, not AssemblyName.dll (which you don't mention in your code at
all). Anyway, if the assembly is in the current directory, I'd think it
should be found, although I'm not 100% sure of it - a look at the docs of
the CreateInstance method should clear this up. For testing purposes it'll
be a very good idea to simply use the complete path to the assembly dll
and see if the problem goes away.

Another problem could be that the method actually does look into the
current directory, but that has been changed since the app start, so it's
no longer the bin directory. Using a tool like filemon
(www.sysinternals.com) makes it easy to find out what exactly your app is
doing.

Last, you'll have to use the fully qualified name of the type you want to
instantiate - and that shouldn't be "MyForm". It it was actually MyForm,
you'd have it outside of all namespaces, which is not a recommended thing
to do.



Oliver Sturm
 
Hi,

Also, you can use the Fusion Log Viewer tool from the Framework SDK
(fuslogvw.exe) to find out where the Framework probes for the assembly in
question.

Plus, things to keep in mind:

a) The first CreateInstance argument is an assembly name, not a file name
(they are not the same!). Use "General.Windows.UI" (omit the last dot and
the file extension).
b) No need to use the "Bin" folder - it is an ASP .NET convention (app
domains created by ASP .NET are pre-configured to look for assemblies in
that folder, but this is not true for Windows Forms applications). Just put
the assembly in question to the same folder where the application executable
resides (as far as I remember, the start-up folder is probed anyway even if
it's not the current directory).
 
Back
Top