Reputation: 471
Well i've seen this being done in another project, which was also using windows forms, and i'm just curious on how it can be done. Basically what i'm asking for example: I have 2 windows form projects, currently there both separate but i'd like to incorporate them into each-other. One big aspect of this will be how to load the 2nd project into the 1st. So i've built my seconded project as a .dll file. And i then want to place that .dll file into a folder inside the project and then be able search for any .dlls in the folder and load them up.
So i know i can do this via adding the .dll file from my 2nd project into the references of the first project, but specifically im wondering if it can be done without this? So that in my first project i can just say; "Search "DirectoryName", if there's any .dll files in that folder, load them up, and add them to a list in the project that the user i.e me can see & hopefully use."
Upvotes: 2
Views: 5044
Reputation: 20050
What you're looking for can be achieved by using a mechanism called Reflection
Reflection allows you to dynamically load an assembly into your application, among other things.
The class you'd need is called Assembly that has several useful methods for loading assemblies:
LoadFile, LoadFrom and a few more.
Here's a code example of loading an assembly at a given path:
string path = @"D:\Folder\MyDll.dll";
Assembly assembly = Assembly.LoadFrom(path);
Once you have loaded an assembly and have a reference to an Assembly object, you can create objects that are defined in it, invoke their methods and so on.
More resources can be found here: Dynamically loading and using Types
Upvotes: 4
Reputation: 481
Here are some useful links.
Asked on SO: here and here External blog example
Basically you will need to load the library, enumerate the types and use Activator.CreateInstance()
to create a new object.
Having interfaces for the classes you need to interact with will help you here too
Upvotes: 0
Reputation: 41088
You may be interested in using the Managed Entity Framework to achieve this.
Alternatively, you can use Assembly.LoadFrom(), as described here.
Upvotes: 1