From 5f31951e7c271a911616dd5ae355b06dc8ee6979 Mon Sep 17 00:00:00 2001 From: Arthur Vickers Date: Wed, 6 Dec 2023 17:57:23 +0000 Subject: [PATCH] Restore option to load assembly into the current AppDomain Fixes #169 Happy to refactor and/or add tests as needed. --- .../CompiledTemplate.TemplateExecutor.cs | 11 +++++++---- .../Mono.TextTemplating/CompiledTemplate.cs | 5 ++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Mono.TextTemplating/Mono.TextTemplating/CompiledTemplate.TemplateExecutor.cs b/Mono.TextTemplating/Mono.TextTemplating/CompiledTemplate.TemplateExecutor.cs index 8fc8a467..3754da0a 100644 --- a/Mono.TextTemplating/Mono.TextTemplating/CompiledTemplate.TemplateExecutor.cs +++ b/Mono.TextTemplating/Mono.TextTemplating/CompiledTemplate.TemplateExecutor.cs @@ -17,13 +17,16 @@ partial class CompiledTemplate class TemplateProcessor : MarshalByRefObject { [SuppressMessage ("Performance", "CA1822:Mark members as static", Justification = "Needs to be an instance for MarshalByRefObject")] - public string CreateAndProcess (ITextTemplatingEngineHost host, CompiledAssemblyData templateAssemblyData, string templateAssemblyFile, string fullName, CultureInfo culture, string[] referencedAssemblyFiles) + public string CreateAndProcess (ITextTemplatingEngineHost host, CompiledAssemblyData templateAssemblyData, string templateAssemblyFile, string fullName, CultureInfo culture, string[] referencedAssemblyFiles, bool loadAssemblyIntoAppDomain) { using var context = new TemplateAssemblyContext (host, referencedAssemblyFiles); - Assembly assembly = templateAssemblyData is not null - ? context.LoadInMemoryAssembly (templateAssemblyData) - : context.LoadAssemblyFile (templateAssemblyFile); + Assembly assembly = + templateAssemblyData is not null + ? loadAssemblyIntoAppDomain + ? Assembly.Load (templateAssemblyData.Assembly) + : context.LoadInMemoryAssembly (templateAssemblyData) + : context.LoadAssemblyFile (templateAssemblyFile); // MS Templating Engine does not care about the type itself // it only requires the expected members to be on the compiled type diff --git a/Mono.TextTemplating/Mono.TextTemplating/CompiledTemplate.cs b/Mono.TextTemplating/Mono.TextTemplating/CompiledTemplate.cs index 80c18a2d..c3d56218 100644 --- a/Mono.TextTemplating/Mono.TextTemplating/CompiledTemplate.cs +++ b/Mono.TextTemplating/Mono.TextTemplating/CompiledTemplate.cs @@ -105,9 +105,12 @@ TemplateProcessor CreateTemplateProcessor () #endif public string Process () + => Process (loadAssemblyIntoAppDomain: false); + + public string Process (bool loadAssemblyIntoAppDomain) { TemplateProcessor processor = CreateTemplateProcessor (); - return processor.CreateAndProcess (host, templateAssemblyData, templateAssemblyFile, templateClassFullName, culture, ReferencedAssemblyFiles); + return processor.CreateAndProcess (host, templateAssemblyData, templateAssemblyFile, templateClassFullName, culture, ReferencedAssemblyFiles, loadAssemblyIntoAppDomain); } public void Dispose ()