从C ++ / CLI加载相互依赖的程序集

时间:2009-12-30 02:07:46

标签: c++-cli filenotfoundexception assembly.load

我想从C ++ / CLI加载两个程序集;程序集A取决于程序集B,两者都是VB.Net项目(3.5)。我希望它们从一个字节数组加载,所以我使用Assembly :: Load(),但是当我尝试从程序集A实例化一个类时,框架忽略了先前加载的程序集B并尝试再次加载它,这会失败,因为它不在搜索路径中。程序集的“名称”是相同的,所以我不知道它为什么会失败。出于测试目的,我的程序直接从编译的图像加载字节,但实际代码将以不同方式加载。这是我的测试代码:

#include "stdafx.h"

using namespace System;
using namespace System::Windows::Forms;
using namespace System::IO;
using namespace System::Reflection;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    array<unsigned char>^   bytes;
    FileStream^     f;

    f = gcnew FileStream(L"c:\\...\\AssemblyB.dll", FileMode::Open);
    bytes = gcnew array<unsigned char>((int)f->Length);
    f->Read( bytes, 0, (int) f->Length );
    f->Close();
    f = nullptr;
    Assembly^   assemblyb = Assembly::Load(bytes);

    f = gcnew FileStream(L"c:\\...\\AssemblyA.dll", FileMode::Open);
    bytes = gcnew array<unsigned char>((int)f->Length);
    f->Read( bytes, 0, (int) f->Length );
    f->Close();
    f = nullptr;
    Assembly^   assemblya = Assembly::Load(bytes);

    bytes = nullptr;

    // Here I get the file not found exception!
    Object^     mf = assemblya->CreateInstance(L"AssemblyA.MainForm");

    // This line is not reached unless I copy assemblyb.dll to my app's folder:
    mf->GetType()->InvokeMember(L"ShowDialog",BindingFlags::Default | BindingFlags::InvokeMethod,
                                mf->GetType()->DefaultBinder, mf, nullptr );

    return 0;
}

错误是:

无法加载文件或程序集“ AssemblyB,Version = 1.0.3650.39903,Culture = neutral,PublicKeyToken = null ”或其中一个依赖项。系统找不到指定的文件。

当我检查assemblyb-&gt; FullName时,它确切地说' AssemblyB,Version = 1.0.3650.39903,Culture = neutral,PublicKeyToken = null '。

当然,如果我将AssemblyB.dll复制到我的测试程序的文件夹中,代码就可以了,但这不是我想要的。

有什么想法吗?

(顺便说一句,我的第二步将尝试使AssemblyA使用我的C ++ / CLI exe将公开的类。)

1 个答案:

答案 0 :(得分:6)

好吧,我只是为自己感到尴尬。一切都在文档中。

// This class is just for holding a managed static variable for assemblyB
ref class Resolver {
public:
    static  Assembly^   assemblyB;
};

// This is the delegate for resolving assemblies
Assembly^ ResolveHandler(Object^ Sender, ResolveEventArgs^ args)
{
    // Warning: this should check the args for the assembly name!
    return Resolver::assemblyB;
}
.
.
.
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    // Set up the handler for the AssemblyResolve event
    AppDomain::CurrentDomain->AssemblyResolve += gcnew ResolveEventHandler( ResolveHandler );
.
.
.
    // Load assemblyb into the static variable available to the resolver delegate
    Resolver::assemblyb = Assembly::Load(bytes);
.
.
.

我希望有人觉得这很有用。 :)