假设我有3个DLL(BlueCar,RedCar,YellowCar),每个DLL都有一个命名类(BlueCarClass等),它们都实现了相同的接口Car,并且都是从同一个命名空间(Car_Choices)构建的.所以DLL在编译之前看起来像这样:
namespace Car_Choices
{
public interface Car
{
void What_Color();
}
public class BlueCarClass : Car
{
public void What_Color()
{
MessageBox.Show('The color is blue.');
}
}
}
Run Code Online (Sandbox Code Playgroud)
DLL的名称将是"BlueCar.dll".
在主程序中,用户选择他们想要的汽车颜色,并根据他们的选择动态加载适当的DLL并运行What_Color().主程序有一个Car接口的副本.现在我有以下,但它不起作用.
static void Main()
{
string car_choice = win_form_list.ToArray()[0]; //gets choice from a drop down
Assembly car_assembly = Assembly.Load(car_choice); //car_choice is BlueCar
Type car_type = car_assembly.GetType("Car");
Car car = Activator.CreateInstance(type) as Car;
car.What_Color();
}
Run Code Online (Sandbox Code Playgroud)
我也试过了
static void Main()
{
string car_choice = win_form_list.ToArray()[0]; //gets choice from a drop down …Run Code Online (Sandbox Code Playgroud)