From c5e35a2ec564579d037cce3577665ffb8fd6301d Mon Sep 17 00:00:00 2001 From: Sergey Gut Date: Thu, 30 Dec 2021 23:42:41 +0400 Subject: [PATCH] Support module dependencies from non-app directory --- Core/Core.cs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/Core/Core.cs b/Core/Core.cs index c6a981e..f404d9a 100644 --- a/Core/Core.cs +++ b/Core/Core.cs @@ -164,14 +164,29 @@ namespace Kruzya.TelegramBot.Core appDomain.AssemblyResolve += delegate(object? sender, ResolveEventArgs args) { - var basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); - var assemblyPath = Path.Combine(basePath, new AssemblyName(args.Name).Name + ".dll"); - if (!File.Exists(assemblyPath)) + var assemblyName = new AssemblyName(args.Name).Name + ".dll"; + + var possiblePaths = new List(); + if (args.RequestingAssembly != null) { - return null; + var modulePath = Path.GetDirectoryName(args.RequestingAssembly.Location); + possiblePaths.Add(modulePath); + possiblePaths.Add(args.RequestingAssembly.Location.Replace(".dll", "")); } - return Assembly.LoadFrom(assemblyPath); + possiblePaths.Add(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)); + possiblePaths.Add(Environment.CurrentDirectory); + + foreach (var basePath in possiblePaths) + { + var assemblyPath = $"{basePath}/{assemblyName}"; + if (File.Exists(assemblyPath)) + { + return Assembly.LoadFrom(assemblyPath); + } + } + + return null; }; } }