Dynamic Class Creation

H

herbert422

I am trying to instantiate a new class using
Activator.CreateInstance(). The class is actually a window's form.
When the process executes the CreateInstance() method it generates an
exception. The exception states that it could not find an assembly.

In the original project that generated the dll, I added a reference to
the dll in the project (via Add Reference in the Solution Explorer
Pane of the IDE). Everything works in that project. Afterwards, I
recompiled it as a dll.

How would I go about solving the linking issue?

Thanks in Advance.
 
M

Marc Gravell

Has the necessary .dll made it into the build-output of your main
project (usually under the "bin" folder)? Note that the compiler will
silently drop references that it detects aren't used by your code,
with the effect that it can get removed from the build-output.
Ensure "Copy Local" is set to true on the reference; if that doesn't
work, and the .dll is in your project-scope, then you can try changing
the build-action to "Component", or setting "Copy to Output Directory"
to true.

If the .dll is already there, then please indicate what are you
passing to Activator.CreateInstance()?

Marc
 
H

herbert422

I tried the settings on the DLL files and it still could not find the
library. However, I included both the files in the resource.resx and
that seemed to have solved the issue of the library not being able to
resolve the missing library.

How I am calling the CreateInstance() method is passing in the type of
the construct. So the following:

Assembly myDLL = Assembly.LoadFile(path)
myObject = Activator.CreateInstance(myDLL.GetType("Namespace.MyClass")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top