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は、異なるフォルダにまだファイル。どのように私はすべての必要なファイルをロードし、関数を呼び出すことができますか?
ありがとうございました。
自分のアセンブリをロードするすべての人は、https://blogs.msdn.microsoft.com/suzcook/tag/loader-info/およびhttps://social.msdn.microsoft.com/search/enから読んでください。あなたは最近それをやった人があなたのための要約を提供してくれることを願っています(あなたは長期的にあなたを助けるつもりはないと考えています - 考えてみましょう。とにかく記事を読んで) –