簡體   English   中英

使用MEF導入C ++ / CLI dll

[英]Import C++/CLI dll using 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 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>();
    }
}

我希望能夠通過其原義名稱或對象名稱(如上所述)加載.dll。
當我調用類型為ReflectionTypeLoadException的 GetExportedValue時,以上代碼引發錯誤:

 InnerException = null   
 Message = "Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information."
 Source = "mscorlib"

另外,在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

我找不到使用接口和托管\\非托管.dll加載的教程。 如果您知道的話,請給我講話。

我應該如何使用它?我在做什么錯?

Fusion Log告訴您出了什么問題。

WRN:不會在LoadFrom上下文中探測本機映像。

C ++ / CLI圖像是混合模式,並且由於加載它可能會有副作用( DllMain執行),因此MEF不會冒險。 您仍然可以顯式加載程序集。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM