4using System.Reflection;
5using System.Runtime.Loader;
8using System.Threading.Tasks;
13 partial class ExecutionContext
19 class ExecutionContextDispatcher
22 readonly
string[] _args;
29 public ExecutionContextDispatcher(
string args)
32 throw new ArgumentNullException(nameof(args));
34 _args = JsonSerializer.Deserialize<
string[]>(args);
43 return Task.Run(() => ImportTool.ExecuteInContext(_args)).GetAwaiter().GetResult();
51 class IsolatedAssemblyLoadContext : AssemblyLoadContext
54 readonly AssemblyDependencyResolver _resolver;
61 public IsolatedAssemblyLoadContext(
string name,
bool isCollectible =
true) :
62 base(name, isCollectible)
64 _resolver =
new AssemblyDependencyResolver(
Assembly.GetEntryAssembly().Location);
69 return _resolver.ResolveAssemblyToPath(assemblyName) is
string p ? base.LoadFromAssemblyPath(p) : base.Load(assemblyName);
74 IsolatedAssemblyLoadContext _context;
82 public ExecutionContext(
string[] args)
85 _context =
new IsolatedAssemblyLoadContext(
"IkvmImporter",
true);
86 var
asm = _context.LoadFromAssemblyName(typeof(ExecutionContextDispatcher).
Assembly.GetName());
87 var typ =
asm.GetType(typeof(ExecutionContextDispatcher).FullName);
88 _dispatcher = Activator.CreateInstance(typ, [JsonSerializer.Serialize(args)]);
97 public partial async Task<int>
ExecuteAsync(CancellationToken cancellationToken)
99 return await Task.Run(() => (
int)_dispatcher.GetType().GetMethod(nameof(ExecutionContextDispatcher.Execute), BindingFlags.Public | BindingFlags.Instance).Invoke(_dispatcher, []));
105 public void Dispose()
110 if (_context !=
null)
118 GC.SuppressFinalize(
this);
IKVM.Reflection.Assembly Assembly
IKVM.Reflection.AssemblyName AssemblyName
partial Task< int > ExecuteAsync(CancellationToken cancellationToken)
Executes the importer logic.