@@ -110,16 +110,15 @@ private static void AssemblyLoadHandler(object ob, AssemblyLoadEventArgs args)
110110 /// </summary>
111111 private static Assembly ResolveHandler ( object ob , ResolveEventArgs args )
112112 {
113- string name = args . Name . ToLower ( ) ;
114- foreach ( Assembly a in assemblies )
113+ var name = new AssemblyName ( args . Name ) ;
114+ foreach ( var alreadyLoaded in assemblies )
115115 {
116- string full = a . FullName . ToLower ( ) ;
117- if ( full . StartsWith ( name ) )
116+ if ( AssemblyName . ReferenceMatchesDefinition ( name , alreadyLoaded . GetName ( ) ) )
118117 {
119- return a ;
118+ return alreadyLoaded ;
120119 }
121120 }
122- return LoadAssemblyPath ( args . Name ) ;
121+ return LoadAssemblyPath ( name . Name ) ;
123122 }
124123
125124
@@ -154,6 +153,17 @@ internal static void UpdatePath()
154153 }
155154 }
156155
156+ /// <summary>
157+ /// Given an assembly name, try to find this assembly file using the
158+ /// PYTHONPATH. If not found, return null to indicate implicit load
159+ /// using standard load semantics (app base directory then GAC, etc.)
160+ /// </summary>
161+ public static string FindAssembly ( AssemblyName name )
162+ {
163+ if ( name is null ) throw new ArgumentNullException ( nameof ( name ) ) ;
164+
165+ return FindAssembly ( name . Name ) ;
166+ }
157167
158168 /// <summary>
159169 /// Given an assembly name, try to find this assembly file using the
@@ -162,8 +172,13 @@ internal static void UpdatePath()
162172 /// </summary>
163173 public static string FindAssembly ( string name )
164174 {
165- char sep = Path . DirectorySeparatorChar ;
175+ if ( name is null ) throw new ArgumentNullException ( nameof ( name ) ) ;
166176
177+ return FindAssemblyCandidates ( name ) . FirstOrDefault ( ) ;
178+ }
179+
180+ static IEnumerable < string > FindAssemblyCandidates ( string name )
181+ {
167182 foreach ( string head in pypath )
168183 {
169184 string path ;
@@ -173,22 +188,21 @@ public static string FindAssembly(string name)
173188 }
174189 else
175190 {
176- path = head + sep + name ;
191+ path = Path . Combine ( head , name ) ;
177192 }
178193
179194 string temp = path + ".dll" ;
180195 if ( File . Exists ( temp ) )
181196 {
182- return temp ;
197+ yield return temp ;
183198 }
184199
185200 temp = path + ".exe" ;
186201 if ( File . Exists ( temp ) )
187202 {
188- return temp ;
203+ yield return temp ;
189204 }
190205 }
191- return null ;
192206 }
193207
194208
0 commit comments