C# 使用MEF导入C++/CLI dll

C# 使用MEF导入C++/CLI dll,c#,dll,c++-cli,mef,C#,Dll,C++ Cli,Mef,我有一个C接口和一个C++\CLI实现,我想在托管代码中使用它 C代码: public interface IEngine { void Run(); } C++\CLI代码: [Export(IEngine::typeid)] public ref class Engine : public IEngine { public: virtual void Run(){} }; C加载器: public class Loader { public IEn

我有一个C接口和一个C++\CLI实现,我想在托管代码中使用它

C代码:

public interface IEngine
{
    void Run();
}
C++\CLI代码:

[Export(IEngine::typeid)]
public ref class Engine : public IEngine
{
    public:
        virtual void Run(){}
};
C加载器:

public class Loader
{
    public IEngine Load()
    {
        var path = @"D:\Engine\Dlls";
        var catalog = new AggregateCatalog(
                    newAssemblyCatalog(Assembly.GetExecutingAssembly().Location), 
                    new DirectoryCatalog(path, "*.dll"));
        var m_container = new CompositionContainer(catalog);
        return m_container.GetExportedValue<IEngine>();
    }
}
此外,在LoaderExceptions中有12个例外情况相同:

[System.IO.FileNotFoundException]   {System.IO.FileNotFoundException: Could not load file or assembly 'GNetTcp, Version=1.0.0.23326, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
File name: 'GNetTcp, Version=1.0.0.23326, Culture=neutral, PublicKeyToken=null'

=== Pre-bind state information ===
LOG: DisplayName = GNetTcp, Version=1.0.0.23326, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///D:/NovaSW/Selective_Modeling_45/Engine/Sources/Algorithms/Algs/SelectiveModeling45Tests/bin/Debug
LOG: Initial PrivatePath = NULL
Calling assembly : ComputationDataTypes, Version=7.2.0.1, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Users\rotem-o\AppData\Local\Temp\4knkp2kk.w3r\60533983-b3f6-4ca4-bb72-9f8ea4a2e2a3.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///D:/NovaSW/Selective_Modeling_45/Engine/Sources/Algorithms/Algs/SelectiveModeling45Tests/bin/Debug/GNetTcp.DLL.
LOG: Attempting download of new URL file:///D:/NovaSW/Selective_Modeling_45/Engine/Sources/Algorithms/Algs/SelectiveModeling45Tests/bin/Debug/GNetTcp/GNetTcp.DLL.
LOG: Attempting download of new URL file:///D:/NovaSW/Selective_Modeling_45/Engine/Sources/Algorithms/Algs/SelectiveModeling45Tests/bin/Debug/GNetTcp.EXE.
LOG: Attempting download of new URL file:///D:/NovaSW/Selective_Modeling_45/Engine/Sources/Algorithms/Algs/SelectiveModeling45Tests/bin/Debug/GNetTcp/GNetTcp.EXE.
LOG: Attempting download of new URL file:///D:/NOVASW/SELECTIVE_MODELING_45/OUTPUT/BINARIES/REFERENCES/DLLSENGINE/GNetTcp.DLL.
LOG: Attempting download of new URL file:///D:/NOVASW/SELECTIVE_MODELING_45/OUTPUT/BINARIES/REFERENCES/DLLSENGINE/GNetTcp/GNetTcp.DLL.
LOG: Attempting download of new URL file:///D:/NOVASW/SELECTIVE_MODELING_45/OUTPUT/BINARIES/REFERENCES/DLLSENGINE/GNetTcp.EXE.
LOG: Attempting download of new URL file:///D:/NOVASW/SELECTIVE_MODELING_45/OUTPUT/BINARIES/REFERENCES/DLLSENGINE/GNetTcp/GNetTcp.EXE.
}   System.IO.FileNotFoundException
我找不到使用interface和托管\非托管.dll加载的教程。如果你知道的话,请告诉我


我应该如何使用它以及我做错了什么?

Fusion Log告诉您哪里出了问题

警告:将不会在LoadFrom上下文中探测本机映像


C++/CLI映像是混合模式,由于加载DllMain执行时可能会有副作用,MEF不会冒险。你仍然可以显式加载程序集。

管理C++标签与C++语言之前的C++语言有关,不应该用于C++和CLI问题。你必须告诉我们更多关于异常的内部异常。很可能这是一件非常简单的事情,比如构建错误的程序集位。@HansPassant,如果您找到任何线索,请查看上面编辑的问题中的异常信息。谢谢你是的,我刚试过。我收到一个错误,无法创建接口实例。
[System.IO.FileNotFoundException]   {System.IO.FileNotFoundException: Could not load file or assembly 'GNetTcp, Version=1.0.0.23326, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
File name: 'GNetTcp, Version=1.0.0.23326, Culture=neutral, PublicKeyToken=null'

=== Pre-bind state information ===
LOG: DisplayName = GNetTcp, Version=1.0.0.23326, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///D:/NovaSW/Selective_Modeling_45/Engine/Sources/Algorithms/Algs/SelectiveModeling45Tests/bin/Debug
LOG: Initial PrivatePath = NULL
Calling assembly : ComputationDataTypes, Version=7.2.0.1, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Users\rotem-o\AppData\Local\Temp\4knkp2kk.w3r\60533983-b3f6-4ca4-bb72-9f8ea4a2e2a3.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///D:/NovaSW/Selective_Modeling_45/Engine/Sources/Algorithms/Algs/SelectiveModeling45Tests/bin/Debug/GNetTcp.DLL.
LOG: Attempting download of new URL file:///D:/NovaSW/Selective_Modeling_45/Engine/Sources/Algorithms/Algs/SelectiveModeling45Tests/bin/Debug/GNetTcp/GNetTcp.DLL.
LOG: Attempting download of new URL file:///D:/NovaSW/Selective_Modeling_45/Engine/Sources/Algorithms/Algs/SelectiveModeling45Tests/bin/Debug/GNetTcp.EXE.
LOG: Attempting download of new URL file:///D:/NovaSW/Selective_Modeling_45/Engine/Sources/Algorithms/Algs/SelectiveModeling45Tests/bin/Debug/GNetTcp/GNetTcp.EXE.
LOG: Attempting download of new URL file:///D:/NOVASW/SELECTIVE_MODELING_45/OUTPUT/BINARIES/REFERENCES/DLLSENGINE/GNetTcp.DLL.
LOG: Attempting download of new URL file:///D:/NOVASW/SELECTIVE_MODELING_45/OUTPUT/BINARIES/REFERENCES/DLLSENGINE/GNetTcp/GNetTcp.DLL.
LOG: Attempting download of new URL file:///D:/NOVASW/SELECTIVE_MODELING_45/OUTPUT/BINARIES/REFERENCES/DLLSENGINE/GNetTcp.EXE.
LOG: Attempting download of new URL file:///D:/NOVASW/SELECTIVE_MODELING_45/OUTPUT/BINARIES/REFERENCES/DLLSENGINE/GNetTcp/GNetTcp.EXE.
}   System.IO.FileNotFoundException