Let's say I have 3 DLLs (BlueCar, RedCar, YellowCar), each of which has a named class (BlueCarClass, etc.) that also implement the same interface, Car, and they are all built from the same same namespace (Car_Choices). Thus, before compiling the DLL looks something like this:
namespace Car_Choices { public interface Car { void What_Color(); } public class BlueCarClass : Car { public void What_Color() { MessageBox.Show('The color is blue.'); } } }
And the DLL name will be "BlueCar.dll".
In the main program, the user selects what color of the car they need, and according to their choice, he dynamically loads only the corresponding DLL and runs What_Color (). The main program has a copy of the car interface. Now I have the following, but it does not work.
static void Main() { string car_choice = win_form_list.ToArray()[0];
I also tried
static void Main() { string car_choice = win_form_list.ToArray()[0];
Any help? Are there any structural changes that I need to make (for example, place each color DLL in my own namespace)? Or I donβt understand how to properly load and use classes from a DLL.
EDIT: Here is a solution that I should work in case someone is looking for a more detailed answer.
PROJECT 1: common interface (as a class library) Car_Interface.cs
namespace Car_Interface { public interface ICar_Interface { char Start_Car(); } }
Compile the DLL link in Car_Interface.dll in the following two projects.
PROJECT 2: Implementation of the car interface as a class library BlueCar.cs
namespace BlueCar_PlugIn { public class BlueCar : Car_Interface.ICar_Interface { public char Start_Car() { MessageBox.Show("Car is started"); } } }
Compile to BlueCar_PlugIn.dll
PROJECT 3: Main program / driver Program.cs
namespace Main_Program { public class Program { static void Main() { Assembly assembly = Assembly.Load(DLL_name);
Now, if you move both DLLs to bin (or where your program has ever been compiled) and run it, it will be able to dynamically load BlueCar_PlugIn.dll, but it does not have to run it (for example, if you have YellowCar_PlugIn.dll, and also RedCar_PlugIn.dll with similar implementations, you will need to download only one file for the program to work).