Skip to content

Commit

Permalink
NugetAssemblyLocator load file in same assembly context (like ModuleF…
Browse files Browse the repository at this point in the history
…inder does)
  • Loading branch information
ky-one committed Nov 9, 2020
1 parent 13fb120 commit 8e03756
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
30 changes: 30 additions & 0 deletions Core.Common.Standard/Helpers/AssemblyHelper.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
2 changes: 1 addition & 1 deletion Core.Common.Standard/KY.Core.Common.Standard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>KY.Core</RootNamespace>
<AssemblyName>KY.Core.Common</AssemblyName>
<Version>4.15.0</Version>
<Version>4.15.1</Version>
<Authors>KY-Programming</Authors>
<Company>KY-Programmingp</Company>
<Product>KY.Core</Product>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=helpers/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=nuget/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
18 changes: 1 addition & 17 deletions Core.Common.Standard/Module/ModuleFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,7 @@ private List<ModuleBase> 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);
Expand Down
2 changes: 1 addition & 1 deletion Core.Common.Standard/Nuget/NugetAssemblyLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8e03756

Please sign in to comment.