2016-08-26 5 views
0

DLLと依存関係をロードする必要があります。私のデータベースでは、すべての依存関係(参照ファイルへのパス)を既に保存しています。異なるフォルダにアセンブリをロードする方法C#

IE:負荷へ

DLL:

  • ID:1
  • 名: "DummyModule.dll"

の依存性:

  • DLL番号: 1
  • パス: "C:\ DLL \ ABC.dll"

AssemblyLoaderクラス:

public class AssemblyLoader : MarshalByRefObject 
{ 
    public void Load(string path) 
    { 
     ValidatePath(path); 

     Assembly.Load(path); 
    } 

    public void LoadFrom(string path) 
    { 
     ValidatePath(path); 

     Assembly.LoadFrom(path); 
    } 

    public void LoadBytes(string path) 
    { 
     ValidatePath(path); 

     var b = File.ReadAllBytes(path); 

     Assembly.Load(b); 
    } 

    public Assembly GetAssembly(string assemblyPath) 
    { 
     try 
     { 
      return Assembly.Load(assemblyPath); 
     } 
     catch (Exception ex) 
     { 
      throw new InvalidOperationException(ex.Message); 
     } 
    } 

    public Assembly GetAssemblyBytes(string assemblyPath) 
    { 
     try 
     { 
      var b = File.ReadAllBytes(assemblyPath); 

      return Assembly.Load(b); 
     } 
     catch (Exception ex) 
     { 
      throw new InvalidOperationException(ex.Message); 
     } 
    } 

    private void ValidatePath(string path) 
    { 
     if (path == null) 
      throw new ArgumentNullException("path"); 

     if (!File.Exists(path)) 
      throw new ArgumentException(String.Format("path \"{0}\" does not exist", path)); 
    } 
} 

メインクラス「DummyModule.dll上記のコードで

static void Main(string[] args) 
    { 
     string file1 = @"1\DummyModule.dll"; 
     string file2 = @"2\PSLData.dll"; 
     string file3 = @"3\Security.dll"; 

     try 
     { 
      AppDomain myDomain = AppDomain.CreateDomain("MyDomain"); 

      var assemblyLoader = (AssemblyLoader)myDomain.CreateInstanceAndUnwrap(typeof(AssemblyLoader).Assembly.FullName, typeof(AssemblyLoader).FullName); 

      assemblyLoader.LoadBytes(file2); 
      assemblyLoader.LoadBytes(file3); 

      var dummy = assemblyLoader.GetAssemblyBytes(file1); 

      foreach (var t in dummy.GetTypes()) 
      { 
       var methodInfo = t.GetMethod("D"); 
       if (methodInfo != null) 
       { 
        var obj = Activator.CreateInstance(t); 
        Console.Write(methodInfo.Invoke(obj, new object[] { }).ToString()); 
       } 
      } 

      AppDomain.Unload(myDomain); 
     } 
     catch (Exception ex) 
     { 
      Console.Write(ex.Message); 
     } 

     Console.ReadKey(); 
    } 

"はメインdll、" PSLData.dll "と" Security.dll "は依存関係です。

私はメソッドを呼び出すと、私の「DummyModule.dll」エラーの「D」が表示されます。

Could not load file or assembly 'DummyModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. 

すべてのDLLは、異なるフォルダにまだファイル。どのように私はすべての必要なファイルをロードし、関数を呼び出すことができますか?

ありがとうございました。

+0

自分のアセンブリをロードするすべての人は、https://blogs.msdn.microsoft.com/suzcook/tag/loader-info/およびhttps://social.msdn.microsoft.com/search/enから読んでください。あなたは最近それをやった人があなたのための要約を提供してくれることを願っています(あなたは長期的にあなたを助けるつもりはないと考えています - 考えてみましょう。とにかく記事を読んで) –

答えて

0

これを使用してみてください...それは私のために働いた..あなたは、アセンブリへの相対パスを使用しているので、問題がある

serviceAgentAssembly =System.Reflection.Assembly.LoadFrom(string.Format(CultureInfo.InvariantCulture, @"{0}\{1}", assemblyPath, assemblyName)); 

foreach (Type objType in serviceAgentAssembly.GetTypes()) 
{ 
    //further loops to get the method 
    //your code to ivoke the function 
} 
+0

この行の私のコードの作業を停止する "Console.Write(methodInfo.Invoke(obj、new object [] {})。ToString());" – jsfelipearaujo

0

「何に対する?」作成した新しいAppDomainとアセンブリの読み込みは、森の中で失われます。作成したAppDomainの同じプローブパスを継承しません。 クラスSystem.AppDomainSetupを見て、そのプロパティは、引数としてAppDomainSetupのインスタンスを取ると、createdomainの形()と一緒にPrivateBinPathをApplicationBase。最も簡単な解決策は、AppDomain.CurrentDomain.SetupInformationによって返されたAppDomainSetupインスタンスを使用することです。現在のドメインは、もちろん、新しいドメインが作成されたアプリケーションです。

関連する問題