Skip to content
Permalink
Browse files

[Reflection] AssemblyContainer: return already loaded assemblies if A…

…ssemblyName matches rather than loading new one (it seems that assembly is requested several times because we are not loading in the proper load context)

This might be something we can improve with .NET Core using AssemblyLoadContext, along with collectible assemblies.
  • Loading branch information
xen2 committed Feb 8, 2019
1 parent ff28be3 commit feb297a828e051e28e83c15e4132c950ebf2f6d9
Showing with 11 additions and 5 deletions.
  1. +11 −5 sources/core/Xenko.Core.Design/Reflection/AssemblyContainer.cs
@@ -119,14 +119,20 @@ public bool UnloadAssembly([NotNull] Assembly assembly)
}

[CanBeNull]
private Assembly LoadAssemblyByName([NotNull] string assemblyName, [NotNull] string searchDirectory)
private Assembly LoadAssemblyByName([NotNull] AssemblyName assemblyName, [NotNull] string searchDirectory)
{
if (assemblyName == null) throw new ArgumentNullException(nameof(assemblyName));

var assemblyPartialPathList = new List<string>();
assemblyPartialPathList.AddRange(KnownAssemblyExtensions.Select(knownExtension => assemblyName + knownExtension));
// First, check the list of already loaded assemblies
{
var matchingAssembly = loadedAssemblies.FirstOrDefault(x => x.Assembly.FullName == assemblyName.FullName);
if (matchingAssembly != null)
return matchingAssembly.Assembly;
}

// Look in search paths
var assemblyPartialPathList = new List<string>();
assemblyPartialPathList.AddRange(KnownAssemblyExtensions.Select(knownExtension => assemblyName.Name + knownExtension));
foreach (var assemblyPartialPath in assemblyPartialPathList)
{
var assemblyFullPath = Path.Combine(searchDirectory, assemblyPartialPath);
@@ -143,7 +149,7 @@ private Assembly LoadAssemblyByName([NotNull] string assemblyName, [NotNull] str
if (dependencies == null)
continue;

if (dependencies.TryGetValue(assemblyName, out var fullPath))
if (dependencies.TryGetValue(assemblyName.Name, out var fullPath))
{
return LoadAssemblyFromPathInternal(fullPath);
}
@@ -303,7 +309,7 @@ private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEven
if (container != null)
{
var assemblyName = new AssemblyName(args.Name);
return container.LoadAssemblyByName(assemblyName.Name, searchDirectory);
return container.LoadAssemblyByName(assemblyName, searchDirectory);
}

return null;

0 comments on commit feb297a

Please sign in to comment.
You can’t perform that action at this time.