Мне нужна загрузка DLL и зависимостей. В моей базе данных я уже сохраняю все зависимости (путь к файлам ссылок).Как загрузить сборки в разных папках C#
IE:
DLL для загрузки:
- ID: 1
- имя: "DummyModule.dll"
Зависимости:
- DLL ID: 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));
}
}
Основной класс:
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();
}
В коде выше «DummyModule.dll «главная dll,« PSLData.dll »и« Security.dll »- это зависимости.
Когда я называю метод «D» моей «DummyModule.dll» ошибка появляется:
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 -US? Rq = site% 3Ablogs.msdn.microsoft.com% 2Fsuzcook & rn = suzcook & query = fusion + статьи журнала ... Надеюсь, кто-то, кто сделал это недавно, предоставит вам сводку (которая на самом деле не поможет вам в долгосрочной перспективе - рассмотрите чтение статей в любом случае) –