From 8e03756d26ff98d5806d860a6851dee3ad6ceddf Mon Sep 17 00:00:00 2001 From: KY Date: Mon, 9 Nov 2020 13:47:07 +0100 Subject: [PATCH] NugetAssemblyLocator load file in same assembly context (like ModuleFinder does) --- .../Helpers/AssemblyHelper.cs | 30 +++++++++++++++++++ .../KY.Core.Common.Standard.csproj | 2 +- ...KY.Core.Common.Standard.csproj.DotSettings | 1 + Core.Common.Standard/Module/ModuleFinder.cs | 18 +---------- .../Nuget/NugetAssemblyLocator.cs | 2 +- 5 files changed, 34 insertions(+), 19 deletions(-) create mode 100644 Core.Common.Standard/Helpers/AssemblyHelper.cs diff --git a/Core.Common.Standard/Helpers/AssemblyHelper.cs b/Core.Common.Standard/Helpers/AssemblyHelper.cs new file mode 100644 index 0000000..3c7c815 --- /dev/null +++ b/Core.Common.Standard/Helpers/AssemblyHelper.cs @@ -0,0 +1,30 @@ +using System; +using System.Reflection; + +namespace KY.Core +{ + public static class AssemblyHelper + { + public static Assembly LoadInSameContext(string path) + { + Type loaderType = Type.GetType("System.Runtime.Loader.AssemblyLoadContext"); + if (loaderType != null) + { + PropertyInfo defaultProperty = loaderType.GetProperty("Default", BindingFlags.Static | BindingFlags.Public); + if (defaultProperty != null && defaultProperty.CanRead) + { + object defaultContext = defaultProperty.GetMethod.Invoke(null, new object[0]); + if (defaultContext != null) + { + MethodInfo loadMethod = defaultContext.GetType().GetMethod("LoadFromAssemblyPath"); + if (loadMethod != null) + { + return (Assembly)loadMethod.Invoke(defaultContext, new object[] { path }); + } + } + } + } + return Assembly.LoadFrom(path); + } + } +} \ No newline at end of file diff --git a/Core.Common.Standard/KY.Core.Common.Standard.csproj b/Core.Common.Standard/KY.Core.Common.Standard.csproj index ae1fedf..0a96f5e 100644 --- a/Core.Common.Standard/KY.Core.Common.Standard.csproj +++ b/Core.Common.Standard/KY.Core.Common.Standard.csproj @@ -4,7 +4,7 @@ netstandard2.0 KY.Core KY.Core.Common - 4.15.0 + 4.15.1 KY-Programming KY-Programmingp KY.Core diff --git a/Core.Common.Standard/KY.Core.Common.Standard.csproj.DotSettings b/Core.Common.Standard/KY.Core.Common.Standard.csproj.DotSettings index 823e1ae..afd50fd 100644 --- a/Core.Common.Standard/KY.Core.Common.Standard.csproj.DotSettings +++ b/Core.Common.Standard/KY.Core.Common.Standard.csproj.DotSettings @@ -1,2 +1,3 @@  + True True \ No newline at end of file diff --git a/Core.Common.Standard/Module/ModuleFinder.cs b/Core.Common.Standard/Module/ModuleFinder.cs index ae09ef8..24abd4f 100644 --- a/Core.Common.Standard/Module/ModuleFinder.cs +++ b/Core.Common.Standard/Module/ModuleFinder.cs @@ -94,23 +94,7 @@ private List LoadSafeFromFile(FileInfo file) Assembly assembly = null; if (!this.SeparateContext) { - Type loaderType = Type.GetType("System.Runtime.Loader.AssemblyLoadContext"); - if (loaderType != null) - { - PropertyInfo defaultProperty = loaderType.GetProperty("Default", BindingFlags.Static | BindingFlags.Public); - if (defaultProperty != null && defaultProperty.CanRead) - { - object defaultContext = defaultProperty.GetMethod.Invoke(null, new object[0]); - if (defaultContext != null) - { - MethodInfo loadMethod = defaultContext.GetType().GetMethod("LoadFromAssemblyPath"); - if (loadMethod != null) - { - assembly = (Assembly)loadMethod.Invoke(defaultContext, new object[] { file.FullName }); - } - } - } - } + assembly = AssemblyHelper.LoadInSameContext(file.FullName); } assembly = assembly ?? Assembly.LoadFile(file.FullName); return this.LoadFrom(assembly); diff --git a/Core.Common.Standard/Nuget/NugetAssemblyLocator.cs b/Core.Common.Standard/Nuget/NugetAssemblyLocator.cs index a08b4cc..f5a9e36 100644 --- a/Core.Common.Standard/Nuget/NugetAssemblyLocator.cs +++ b/Core.Common.Standard/Nuget/NugetAssemblyLocator.cs @@ -139,7 +139,7 @@ private Assembly TryFind(AssemblyInfo info, params string[] chunks) if (FileSystem.FileExists(file)) { Logger.Trace($"Assembly found in: {file}"); - return Assembly.LoadFrom(file); + return AssemblyHelper.LoadInSameContext(file); } Logger.Trace($"Assembly searched in: {file}"); return null;