Levon Chen 的脚步

生活在于拥抱改变。

应用程序和COM库不在同一个目录下,但是COM库依赖于另一个C#的库。

 

#using <mscorlib.dll>
#using <System.dll>
#using <System.Xml.dll>


ref class AssemblyResolveProxy
{
public:
 AssemblyResolveProxy() {
  System::AppDomain^ currentDomain = System::AppDomain::CurrentDomain;
  currentDomain->AssemblyResolve += gcnew System::ResolveEventHandler(this, &AssemblyResolveProxy::currentDomain_AssemblyResolve);
 }

private:
 System::Reflection::Assembly^ currentDomain_AssemblyResolve(System::Object^ sender, System::ResolveEventArgs^ args)
 {
  System::String^ myPath = System::Reflection::Assembly::GetExecutingAssembly()->Location;
  System::String^ myDir = System::IO::Path::GetDirectoryName(myPath);
  ////System::String split[] = nullptr;
  //System::String^ symbol = ",";
  //System::Char cSymbol[] = symbol->ToCharArray();
  //System::String split[]  = args->Name->Split(cSymbol); // TODO: is there a better way?  AssemblyName.Parse() doesn't exist
  //System::String^ simpleAssemblyName = split[0];

  System::String^ pAssemplyInfo = args->Name;
  cli::array<System::String^> ^tempItems = pAssemplyInfo->Split(',');

  System::String^ pAssemplyName = tempItems[0];

  System::String^ resolveAssemblyPath = System::IO::Path::Combine(myDir, (gcnew System::Text::StringBuilder(pAssemplyName))->Append(/*MSG0*/".dll")->ToString());

  if (System::IO::File::Exists(resolveAssemblyPath))
   return System::Reflection::Assembly::LoadFrom(resolveAssemblyPath);
  return nullptr;
 }
};

class AssemblyResolver
{
 gcroot<AssemblyResolveProxy^> m_AssemblyResolve;
public:
 AssemblyResolver()
 {
  m_AssemblyResolve = gcnew AssemblyResolveProxy();
 }
};

static AssemblyResolver m_assemblyResolver;

 

posted on 2009-11-05 16:25  Levon Chen  阅读(251)  评论(0编辑  收藏  举报

导航