我正在使用Process.Start()
方法根据需要启动特定的可执行文件。可执行文件是量身定制的,其中有许多,以及路径&名称与他们所做的工作一起在数据库中。
如果它们都实现了接口,是否还有其他方法可以在进程中启动它们?我希望能够调试它们并在调用它们时具有更好的类型安全性。
我可以在每个解决方案中设置一个库项目,然后实例化该库并让它完成工作而不是可执行文件。问题是关于一种机制,它允许我在接口上构建,然后在运行时按名称加载库。
答案 0 :(得分:0)
如果您想要自定义解决方案,可以使用以下内容。否则,您可以使用Prism with Unity or MEF中实现的模块化模式。
public interface IPlugin
{
void Start();
}
// You can change this to support loading based on dll file name
// Use one of the other available Assembly load options
public void Load(string fullAssemblyName)
{
var assembly = System.Reflection.Assembly.Load(fullAssemblyName);
// Assuming only one class implements IPlugin
var pluginType = assembly.GetTypes()
.FirstOrDefault(t => t.GetInterfaces()
.Any(i=> i == typeof(IPlugin)));
// Concrete class implementing IPlugin must have default empty constructor
// for following instance creation to work
var plugin = Activator.CreateInstance(pluginType) as IPlugin;
plugin.Start();
}