我正在学习如何将DLL动态加载到C#程序中。思路是,DLL将包含一个接口和若干个实现该接口的类,这样如果我想要添加新的实现,就不必重新编译整个项目。
于是我创建了这个测试。以下是我的DLL文件:
namespace TestDLL
{
public interface Action
{
void DoAction();
}
public class PrintString : Action
{
public void DoAction()
{
Console.WriteLine("Hello World!");
}
}
public class PrintInt : Action
{
public void DoAction()
{
Console.WriteLine("Hello 1!");
}
}
}
而在我的主程序中,我尝试做如下操作:
static void Main(string[] args)
{
List<Action> actions = new List<Action>();
Assembly myDll = Assembly.LoadFrom("TestDLL.dll");
Type[] types = myDll.GetExportedTypes();
for (int i = 0; i < types.Length; i++)
{
Type type = types[i];
if (type.GetInterface("TestDLL.Action") != null && type != null)
{
Action new_action = myDll.CreateInstance(type.FullName) as Action;
if (new_action != null)
actions.Add(new_action);
else
Console.WriteLine("New Action is NULL");
}
}
foreach (Action action in actions)
action.DoAction();
}
我遇到的问题是,尽管
type.FullName
包含正确的值(例如"TestDLL.PrintString"),
这行代码
myDll.CreateInstance(type.FullName) as Action
始终返回 null。
我不确定问题是什么,或者我该如何解决它。
就像例子中一样,我希望能够向 DLL 添加新的 Action 实现,并使主程序在每个实现上调用 DoAction(),而无需重新编译原始程序。希望这样说得清楚!
Action
,在as Action
中定义在哪里?你是在引用第一个代码所代表的程序集,还是Action
被声明了两次,一次在从TestDLL.dll加载的程序集中,一次在你的主项目中? - Lasse V. Karlsen