9
9
10
10
using System ;
11
11
using System . Reflection ;
12
+ using System . Collections . Generic ;
12
13
using Python . Runtime ;
13
14
14
15
namespace Python . Runtime {
@@ -19,6 +20,9 @@ private PythonConsole() {}
19
20
20
21
[ STAThread ]
21
22
public static int Main ( string [ ] args ) {
23
+ // reference the static assemblyLoader to stop it being optimized away
24
+ AssemblyLoader a = assemblyLoader ;
25
+
22
26
string [ ] cmd = Environment . GetCommandLineArgs ( ) ;
23
27
PythonEngine . Initialize ( ) ;
24
28
@@ -31,16 +35,26 @@ public static int Main(string[] args) {
31
35
// Register a callback function to load embedded assmeblies.
32
36
// (Python.Runtime.dll is included as a resource)
33
37
private sealed class AssemblyLoader {
38
+ Dictionary < string , Assembly > loadedAssemblies ;
39
+
34
40
public AssemblyLoader ( ) {
41
+ loadedAssemblies = new Dictionary < string , Assembly > ( ) ;
42
+
35
43
AppDomain . CurrentDomain . AssemblyResolve += ( sender , args ) => {
36
44
String resourceName = new AssemblyName ( args . Name ) . Name + ".dll" ;
37
45
46
+ if ( loadedAssemblies . ContainsKey ( resourceName ) ) {
47
+ return loadedAssemblies [ resourceName ] ;
48
+ }
49
+
38
50
// looks for the assembly from the resources and load it
39
51
using ( var stream = Assembly . GetExecutingAssembly ( ) . GetManifestResourceStream ( resourceName ) ) {
40
52
if ( stream != null ) {
41
53
Byte [ ] assemblyData = new Byte [ stream . Length ] ;
42
54
stream . Read ( assemblyData , 0 , assemblyData . Length ) ;
43
- return Assembly . Load ( assemblyData ) ;
55
+ Assembly assembly = Assembly . Load ( assemblyData ) ;
56
+ loadedAssemblies [ resourceName ] = assembly ;
57
+ return assembly ;
44
58
}
45
59
}
46
60
0 commit comments