我正在爲MVC 3應用程序中的小部件的插件接口工作。 小工具應該包含自己的模型,視圖和控制器。然後,我希望能夠將一個(mvc-project「Widget」)dll放入我的主應用程序的插件文件夾中。
但是,當它動態加載時,我該如何引用它?我不能像myapp.plugin.clock那樣做,然後我得到編譯錯誤的程序集在rumtime加載!?我應該使用動態嗎?MVC 3插件程序集,如何在加載時使用它?
這是我用來加載插件的代碼,它似乎工作。現在我不知道如何以一種好的方式使用加載的程序集,有什麼想法?
[assembly: PreApplicationStartMethod(typeof(MyApp.PluginActivator), "Initialize")]
public class PluginActivator
{
private static readonly DirectoryInfo PluginFolderInfo;
static PluginActivator()
{
PluginFolderInfo = new DirectoryInfo(HostingEnvironment.MapPath("~/plugins"));
}
public static void Initialize()
{
CopyPluginDlls(PluginFolderInfo, AppDomain.CurrentDomain.DynamicDirectory);
LoadPluginAssemblies(AppDomain.CurrentDomain.DynamicDirectory);
}
private static void CopyPluginDlls(DirectoryInfo sourceFolder, string destinationFolder)
{
foreach (var plug in sourceFolder.GetFiles("*.dll", SearchOption.AllDirectories))
{
if (!File.Exists(Path.Combine(destinationFolder, plug.Name)))
{
File.Copy(plug.FullName, Path.Combine(destinationFolder, plug.Name), false);
}
}
}
public static void LoadPluginAssemblies(string dynamicDirectory)
{
foreach (var plug in Directory.GetFiles(dynamicDirectory, "*.dll", SearchOption.AllDirectories))
{
Assembly assembly = Assembly.Load(AssemblyName.GetAssemblyName(plug));
BuildManager.AddReferencedAssembly(assembly);
}
}
}
? –