99
1010using System ;
1111using System . Reflection ;
12+ using System . Collections . Generic ;
1213using Python . Runtime ;
1314
1415namespace Python . Runtime {
@@ -19,6 +20,9 @@ private PythonConsole() {}
1920
2021 [ STAThread ]
2122 public static int Main ( string [ ] args ) {
23+ // reference the static assemblyLoader to stop it being optimized away
24+ AssemblyLoader a = assemblyLoader ;
25+
2226 string [ ] cmd = Environment . GetCommandLineArgs ( ) ;
2327 PythonEngine . Initialize ( ) ;
2428
@@ -31,16 +35,26 @@ public static int Main(string[] args) {
3135 // Register a callback function to load embedded assmeblies.
3236 // (Python.Runtime.dll is included as a resource)
3337 private sealed class AssemblyLoader {
38+ Dictionary < string , Assembly > loadedAssemblies ;
39+
3440 public AssemblyLoader ( ) {
41+ loadedAssemblies = new Dictionary < string , Assembly > ( ) ;
42+
3543 AppDomain . CurrentDomain . AssemblyResolve += ( sender , args ) => {
3644 String resourceName = new AssemblyName ( args . Name ) . Name + ".dll" ;
3745
46+ if ( loadedAssemblies . ContainsKey ( resourceName ) ) {
47+ return loadedAssemblies [ resourceName ] ;
48+ }
49+
3850 // looks for the assembly from the resources and load it
3951 using ( var stream = Assembly . GetExecutingAssembly ( ) . GetManifestResourceStream ( resourceName ) ) {
4052 if ( stream != null ) {
4153 Byte [ ] assemblyData = new Byte [ stream . Length ] ;
4254 stream . Read ( assemblyData , 0 , assemblyData . Length ) ;
43- return Assembly . Load ( assemblyData ) ;
55+ Assembly assembly = Assembly . Load ( assemblyData ) ;
56+ loadedAssemblies [ resourceName ] = assembly ;
57+ return assembly ;
4458 }
4559 }
4660
0 commit comments