Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

AssemblyLoadContext und Reflection #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/shared/DotNetClasses/CSharpCodeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections.Immutable;
using System.IO;
using System.Reflection;
using System.Runtime.Loader;
using System.Text;
using FastReport.Code.CodeDom.Compiler;

Expand All @@ -33,6 +34,7 @@ public override CompilerResults CompileAssemblyFromSource(CompilerParameters cp,
CSharpCompilationOptions options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary,
optimizationLevel: OptimizationLevel.Release,
generalDiagnosticOption: ReportDiagnostic.Default,
assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default,
reportSuppressedDiagnostics: true);

List<MetadataReference> references = new List<MetadataReference>();
Expand All @@ -53,9 +55,26 @@ public override CompilerResults CompileAssemblyFromSource(CompilerParameters cp,
EmitResult results = compilation.Emit(ms);
if (results.Success)
{
ms.Seek(0, SeekOrigin.Begin);
var compiledAssembly = ms.ToArray();
Assembly assembly;
using (var asm = new MemoryStream(compiledAssembly))
{
if (AssemblyLoadContext.CurrentContextualReflectionContext != null)
{
assembly =
AssemblyLoadContext.CurrentContextualReflectionContext.LoadFromStream(asm);
}
else
{
assembly = AssemblyLoadContext.Default.LoadFromStream(asm);
}
}

// assembly = Assembly.Load(ms.ToArray());
return new CompilerResults()
{
CompiledAssembly = Assembly.Load(ms.ToArray())
CompiledAssembly = assembly
};
}
else
Expand Down
28 changes: 25 additions & 3 deletions src/shared/DotNetClasses/CodeDomProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,27 @@ public MetadataReference GetReference(string refDll)
try
{
// try load Assembly in runtime (for user script with custom assembly)
AssemblyLoadContext.Default.LoadFromAssemblyPath(refDll);
if (AssemblyLoadContext.CurrentContextualReflectionContext != null)
{
AssemblyLoadContext.CurrentContextualReflectionContext.LoadFromAssemblyPath(refDll);
}
else
{
AssemblyLoadContext.Default.LoadFromAssemblyPath(refDll);
}
}
catch(ArgumentException) {
var fullpath = Path.Combine(Environment.CurrentDirectory, refDll);
try
{
AssemblyLoadContext.Default.LoadFromAssemblyPath(fullpath);
if (AssemblyLoadContext.CurrentContextualReflectionContext != null)
{
AssemblyLoadContext.CurrentContextualReflectionContext.LoadFromAssemblyPath(fullpath);
}
else
{
AssemblyLoadContext.Default.LoadFromAssemblyPath(fullpath);
}
}
catch { }
}
Expand All @@ -260,7 +274,15 @@ public MetadataReference GetReference(string refDll)

#if NETCOREAPP
// try load Assembly in runtime (for user script with custom assembly)
var assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(assemblyName);
Assembly assembly;
if (AssemblyLoadContext.CurrentContextualReflectionContext != null)
{
assembly = AssemblyLoadContext.CurrentContextualReflectionContext.LoadFromAssemblyName(assemblyName);
}
else
{
assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(assemblyName);
}
#else
var assembly = Assembly.Load(assemblyName);
#endif
Expand Down
20 changes: 19 additions & 1 deletion src/shared/DotNetClasses/VBCodeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.Loader;
using System.Text;
using FastReport.Code.CodeDom.Compiler;

Expand Down Expand Up @@ -42,9 +43,26 @@ public override CompilerResults CompileAssemblyFromSource(CompilerParameters cp,
EmitResult results = compilation.Emit(ms);
if (results.Success)
{
ms.Seek(0, SeekOrigin.Begin);
var compiledAssembly = ms.ToArray();
Assembly assembly;
using (var asm = new MemoryStream(compiledAssembly))
{
if (AssemblyLoadContext.CurrentContextualReflectionContext != null)
{
assembly =
AssemblyLoadContext.CurrentContextualReflectionContext.LoadFromStream(asm);
}
else
{
assembly = AssemblyLoadContext.Default.LoadFromStream(asm);
}
}

// assembly = Assembly.Load(ms.ToArray());
return new CompilerResults()
{
CompiledAssembly = Assembly.Load(ms.ToArray())
CompiledAssembly = assembly
};
}
else
Expand Down