8000 Reference the assembly loader in the Main method to stop it being opt… · pythonindy/pythonnet@10a2f8f · GitHub
[go: up one dir, main page]

Skip to content

Commit 10a2f8f

Browse files
committed
Reference the assembly loader in the Main method to stop it being optimized away, and don't reload assemblies once they're already loaded.
1 parent 0ac66b3 commit 10a2f8f

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

pythonnet/src/console/pythonconsole.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
using System;
1111
using System.Reflection;
12+
using System.Collections.Generic;
1213
using Python.Runtime;
1314

1415
namespace 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

Comments
 (0)
0