8
8
9
9
namespace Peachpie . Library . Scripting
10
10
{
11
- class PhpCompilationFactory : System . Runtime . Loader . AssemblyLoadContext
11
+ #if NETSTANDARD
12
+
13
+ sealed class PhpCompilationFactory : PhpCompilationFactoryBase
14
+ {
15
+ System . Runtime . Loader . AssemblyLoadContext AssemblyLoadContext => System . Runtime . Loader . AssemblyLoadContext . Default ;
16
+
17
+ public PhpCompilationFactory ( )
18
+ {
19
+ //AssemblyLoadContext.Resolving += AssemblyLoadContext_Resolving;
20
+ }
21
+
22
+ //private Assembly AssemblyLoadContext_Resolving(System.Runtime.Loader.AssemblyLoadContext assCtx, AssemblyName assName)
23
+ // => TryGetSubmissionAssembly(assName);
24
+
25
+ protected override Assembly LoadFromStream ( MemoryStream peStream , MemoryStream pdbStream )
26
+ => AssemblyLoadContext . LoadFromStream ( peStream , pdbStream ) ;
27
+ }
28
+
29
+ #else // NET461, does not have AssemblyLoadContext in System.Runtime.Loader 4.0.0 (we need >= 4.0.1)
30
+
31
+ sealed class PhpCompilationFactory : PhpCompilationFactoryBase
12
32
{
13
33
public PhpCompilationFactory ( )
34
+ {
35
+ AppDomain . CurrentDomain . AssemblyResolve += CurrentDomain_AssemblyResolve ;
36
+ }
37
+
38
+ private Assembly CurrentDomain_AssemblyResolve ( object sender , ResolveEventArgs args )
39
+ => TryGetSubmissionAssembly ( new AssemblyName ( args . Name ) ) ;
40
+
41
+ protected override Assembly LoadFromStream ( MemoryStream peStream , MemoryStream pdbStream )
42
+ => Assembly . Load ( peStream . ToArray ( ) , pdbStream ? . ToArray ( ) ) ;
43
+ }
44
+
45
+ #endif
46
+
47
+ abstract class PhpCompilationFactoryBase
48
+ {
49
+ public PhpCompilationFactoryBase ( )
14
50
{
15
51
_compilation = PhpCompilation . Create ( "project" ,
16
52
references : MetadataReferences ( ) . Select ( CreateMetadataReference ) ,
@@ -89,6 +125,8 @@ public Assembly TryGetSubmissionAssembly(AssemblyName assemblyName)
89
125
}
90
126
}
91
127
128
+ protected abstract Assembly LoadFromStream ( MemoryStream peStream , MemoryStream pdbStream ) ;
129
+
92
130
public Assembly LoadFromStream ( AssemblyName assemblyName , MemoryStream peStream , MemoryStream pdbStream )
93
131
{
94
132
var assembly = LoadFromStream ( peStream , pdbStream ) ;
@@ -100,8 +138,6 @@ public Assembly LoadFromStream(AssemblyName assemblyName, MemoryStream peStream,
100
138
return assembly ;
101
139
}
102
140
103
- protected override Assembly Load ( AssemblyName assemblyName ) => TryGetSubmissionAssembly ( assemblyName ) ;
104
-
105
141
static int _counter = 0 ;
106
142
107
143
const string s_submissionAssemblyNamePrefix = "<submission>`" ;
0 commit comments