单声道 - 加载外部dll与它的依赖关系

时间:2015-10-12 12:35:02

标签: c# .net mono

我在Mono行为方面遇到了严重问题。我的应用程序使用 System.Component.Composition 来加载插件。这些插件位于我的程序的子文件夹中。让我告诉你:

+ProgramFolder
--Program.exe
--ProgramCore.dll
-+PluginsFolder
--+Plugin1Folder
----Plugin1.dll
----SomeLibrary.dll (it's dependence which is used by Plugin1.dll)
---Plugin2

在本机Windows环境中运行时效果非常好。但是,只要我想在MONO环境中运行它就会失败并显示错误

Could not load file or assembly 'Plugin1' or one of its dependencies. The system cannot find the file specified.

在我的app.config中,我放置了程序集绑定部分这样的探测

<probing privatePath="PluginsFolder/Plugin1Folder"/>

允许我的程序在这里搜索dll,但看起来MONO忽略了这一部分。如何让它在MONO中运作?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。这是我第二次看到MONO与本机Windows环境完全不同。我不得不重写方法,现在,它可以在预期的两种环境下工作。

所以这是具有适当方法的类:

class ModulesLoader
{
    [ImportMany(typeof(ISchedulable))]
    public IEnumerable<ISchedulable> Modules { get; set; }
    private string dir;
    private string name;

    public ModulesLoader(string directory, string name)
    {
        Assert.ThrowIfEmptyString(directory);
        Assert.ThrowIfNull (name);
        this.dir = directory;
        this.name = name;
    }

    public void Load()
    {
        var catalog = new AggregateCatalog();
        var assembly = GetType ().Assembly; //Assembly of our executable program
        var dllCatalog = new AssemblyCatalog (assembly);
        catalog.Catalogs.Add(dllCatalog);
        var pluginAssembly = Assembly.LoadFrom (Path.Combine (dir, name)); //We have to explicit point which assembly will be loaded
        var pluginCatalog = new AssemblyCatalog (pluginAssembly); //pass it to catalog to let framework know each vital information about this .dll
        catalog.Catalogs.Add (pluginCatalog);
        CompositionContainer container = new CompositionContainer(catalog);
        container.ComposeParts(this); //and it works!
    }
}

也有第二个问题。 System.Configuration dll未标记为&#39;复制本地&#39;和MONO抛出InvalidProgramException,它通知该方法体是空的。最后,我设法让它工作xD

编写平台不可知软件有时候真的很奇怪xD