在引用与同一目录中的另一个文件具有相同名称(在.dll之前的部分)的程序集时,我遇到了 IronPython 的问题。
例如,如果Foo.xml和Foo.Xml.dll位于同一目录中,则该目录已附加到sys.path并尝试引用程序集Foo.Xml:
clr.AddReference("Foo.Xml")
出现以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: System.IO.IOException: Could not add reference to assembly Foo.Xml
at IronPython.Runtime.ClrModule.AddReference(CodeContext context, String name)
at IronPython.Runtime.ClrModule.AddReference(CodeContext context, Object[] references)
at Microsoft.Scripting.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`6.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
at IronPython.Compiler.Ast.CallExpression.Invoke1Instruction.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
at IronPython.Hosting.PythonCommandLine.<>c__DisplayClass1.<RunOneInteraction>b__0()
现在,只需在调用中包含.dll扩展名即可避免这种情况。AddReference()
但是,当被引用的程序集的依赖项出现歧义时,这将不再由您控制。例如,假设程序集Foo.dll依赖于Foo.Xml.dll。当 IronPython 试图在内部解决这个依赖时,它会遇到前面提到的名称冲突并失败。该错误稍微不那么冗长,但问题似乎是相同的:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] Could not load file or assembly 'Foo.Xml, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
在这两种情况下,更改Foo.xml文件的名称是成功加载Foo.Xml.dll程序集与失败之间的区别。
有谁之前经历过这个吗?有人有解决方法吗?更好的方法?