Skip to content

Commit

Permalink
Added isolated assembly loading for issue MapsterMapper#714
Browse files Browse the repository at this point in the history
  • Loading branch information
boylec committed Jun 14, 2024
1 parent 09a0e92 commit 1961db5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,8 @@ project.lock.json
/src/.vs
/.vs
src/.idea

# VS Code settings
.vscode/launch.json
.vscode/settings.json
.vscode/tasks.json
48 changes: 48 additions & 0 deletions src/Mapster.Tool/IsolatedAssemblyLoadContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Loader;

namespace Mapster.Tool
{
public class IsolatedAssemblyContext : AssemblyLoadContext
{
private readonly AssemblyDependencyResolver resolver;

public IsolatedAssemblyContext(string assemblyPath)
{
resolver = new AssemblyDependencyResolver(assemblyPath);
}

protected override Assembly Load(AssemblyName assemblyName)
{
string assemblyPath = resolver.ResolveAssemblyToPath(assemblyName);
if (assemblyPath != null)
{
return LoadFromAssemblyPath(assemblyPath);
}

return null;
}

protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
{
string libraryPath = resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
if (libraryPath != null)
{
return LoadUnmanagedDllFromPath(libraryPath);
}

return IntPtr.Zero;
}

public static Assembly LoadAssemblyFrom(string assemblyPath)
{
Console.WriteLine($"Loading assembly from {assemblyPath}");
IsolatedAssemblyContext loadContext = new IsolatedAssemblyContext(assemblyPath);
return loadContext.LoadFromAssemblyName(
new AssemblyName(Path.GetFileNameWithoutExtension(assemblyPath))
);
}
}
}
6 changes: 3 additions & 3 deletions src/Mapster.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private static void WriteFile(string code, string path)

private static void GenerateMappers(MapperOptions opt)
{
var assembly = Assembly.LoadFrom(Path.GetFullPath(opt.Assembly));
var assembly = IsolatedAssemblyContext.LoadAssemblyFrom(Path.GetFullPath(opt.Assembly));
var config = TypeAdapterConfig.GlobalSettings;
config.SelfContainedCodeGeneration = true;
config.Scan(assembly);
Expand Down Expand Up @@ -150,7 +150,7 @@ private static string GetImplName(string name)

private static void GenerateModels(ModelOptions opt)
{
var assembly = Assembly.LoadFrom(Path.GetFullPath(opt.Assembly));
var assembly = IsolatedAssemblyContext.LoadAssemblyFrom(Path.GetFullPath(opt.Assembly));
var codeGenConfig = new CodeGenerationConfig();
codeGenConfig.Scan(assembly);

Expand Down Expand Up @@ -381,7 +381,7 @@ private static void ApplySettings(TypeAdapterSetter setter, BaseAdaptAttribute a

private static void GenerateExtensions(ExtensionOptions opt)
{
var assembly = Assembly.LoadFrom(Path.GetFullPath(opt.Assembly));
var assembly = IsolatedAssemblyContext.LoadAssemblyFrom(Path.GetFullPath(opt.Assembly));
var config = TypeAdapterConfig.GlobalSettings;
config.SelfContainedCodeGeneration = true;
config.Scan(assembly);
Expand Down

0 comments on commit 1961db5

Please sign in to comment.