|
1 | 1 | using Json.Schema.Generation.XmlComments;
|
2 | 2 | using Microsoft.CodeAnalysis;
|
3 | 3 | using Microsoft.CodeAnalysis.CSharp;
|
4 |
| -using System.Reflection; |
5 |
| -using Json.Schema.Generation; |
| 4 | +using System.Runtime.Loader; |
6 | 5 | using static LearnJsonEverything.Services.Iconography;
|
7 | 6 |
|
8 | 7 | namespace LearnJsonEverything.Services;
|
9 | 8 |
|
10 | 9 | public static class CompilationHelpers
|
11 | 10 | {
|
| 11 | + private static AssemblyLoadContext? _assemblyLoadContext; |
12 | 12 | private static MetadataReference[]? _references;
|
13 | 13 | private static bool _isLoading;
|
14 | 14 |
|
@@ -106,21 +106,26 @@ public static (ILessonRunner<T>?, string[]) GetRunner<T>(LessonData lesson)
|
106 | 106 | return (null, [.. diagnostics]);
|
107 | 107 | }
|
108 | 108 |
|
| 109 | + dllStream.Position = 0; |
| 110 | + pdbStream.Position = 0; |
| 111 | + xmlStream.Position = 0; |
| 112 | + |
109 | 113 | #pragma warning disable IL2026
|
110 | 114 | #pragma warning disable IL2072
|
111 | 115 | #pragma warning disable IL2070
|
112 |
| -#pragma warning disable CS0618 |
113 |
| - var assembly = Assembly.Load(dllStream.ToArray()); |
| 116 | + _assemblyLoadContext?.Unload(); |
| 117 | + _assemblyLoadContext = new AssemblyLoadContext(nameof(CompilationHelpers), true); |
| 118 | + var assembly = _assemblyLoadContext.LoadFromStream(dllStream, pdbStream); |
114 | 119 |
|
115 |
| - xmlStream.Position = 0; |
116 | 120 | using var reader = new StreamReader(xmlStream);
|
117 | 121 | var xmlContent = reader.ReadToEnd();
|
| 122 | +#pragma warning disable CS0618 |
118 | 123 | DocXmlReader.ExplicitlyAddAssemblyXml(assembly, xmlContent);
|
| 124 | +#pragma warning restore CS0618 |
119 | 125 |
|
120 | 126 |
|
121 | 127 | var type = assembly.DefinedTypes.Single(x => !x.IsInterface && x.ImplementedInterfaces.Contains(typeof(ILessonRunner<T>)));
|
122 | 128 | var runner = (ILessonRunner<T>)Activator.CreateInstance(type)!;
|
123 |
| -#pragma warning restore CS0618 |
124 | 129 | #pragma warning restore IL2070
|
125 | 130 | #pragma warning restore IL2072
|
126 | 131 | #pragma warning restore IL2026
|
|
0 commit comments