如何在Iron Python脚本中加载可移植的.NET库? [英] How to load portable .NET library within Iron Python script?

查看:98
本文介绍了如何在Iron Python脚本中加载可移植的.NET库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难从Python脚本加载可移植的.NET库(用于标准.NET和Silverlight环境).

I have serious troubles loading a portable .NET library (to be used in standard .NET and Silverlight environment) from a Python script.

.NET DLL文件的版本是4.0.3.319.233(System.Core.DLL),IronPython是2.7.1,以32bit/x86模式运行. .NET 4下具有C#的Visual Studio2010.还安装了可移植库用法的Microsoft .NET更新KB2468871(版本2).

.NET DLL file version is 4.0.3.319.233 (System.Core.DLL), IronPython is 2.7.1, running in 32bit/x86 mode. Visual Studio 2010 with C# under .NET 4. Microsoft .NET update KB2468871 for Portable library usage is also installed (Version 2).

如果我尝试从Python脚本加载该库:

If I try to load the library from the Python script:

clr.AddReferenceToFileAndPath(UsedPath+"\\MyNamespace\\MyPortableLibrary.dll")

它不能被访问,并且当脚本到达一个类型时,它说: 名称空间#"的属性[便携式组件中的类型]是只读的" 表示程序集根本没有加载(或作为Silverlight加载,并且不能被Python脚本使用).

it can't be accessed, and when the script reaches a type, it says: "attribute [type in portable assembly] of 'namespace#' is read-only" indicating the assembly has not been loaded at all (or as Silverlight, and can't be used by the Python script).

将代码更改为:(来自System.Reflection的Assembly类)

Changing the code to: (Assembly class from System.Reflection)

PortableAssembly = Assembly.LoadFrom(UsedPath+"\\MyNamespace\\MyPortableLibrary.dll") # load through .NET Reflection, Python won't load Portable assembly properly!
clr.AddReference(PortableAssembly)

导致错误: 发生 exceptions.IOError 消息:[Errno 2]无法加载文件或程序集'System.Core,版本= 2.0.5.0,区域性=中性,PublicKeyToken = 7cec85d7bea7798e,Retargetable =是'或其依赖项之一.系统找不到指定的文件.

results in an error: exceptions.IOError occurred Message: [Errno 2] Could not load file or assembly 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes' or one of its dependencies. The system cannot find the file specified.

当从另一个.NET程序中自动调用Python脚本实例化它自己的Python引擎时,最后一个代码似乎起作用,但是当从Visual Studio中的Python项目执行该脚本时,会出现上述错误. VisualStudio,Tools \ Options \ Python Tools \ Interpreter Options中的Python设置适用于x86/32bit模式.所有环境参数均表明已使用.NET 4.

The last code seems to work, when the Python script is called automatically from within another .NET program, instantiating it's own Python engine, but gives the above error when the script is executed from a Python project in Visual Studio. Python settings in VisualStudio, Tools\Options\Python Tools\Interpreter Options are for x86/32bit mode. All environment parameters show that .NET 4 is used.

我现在有多种方法可以通过C#/.NET生成的Python引擎对其进行修复,但是我如何在基本的IronPython运行时环境中加载可移植程序集,以使其在正确的.NET 4环境中工作,不尝试加载任何.NET 2内容?

I've got multiple ways now to fix it from a C#/.NET generated Python engine, but how can I load the portable assembly in a basic IronPython runtime environment, so that it works in the correct .NET 4 environment, not trying to load any .NET 2 stuff?

更新: MS KB2468871更新后,我重新启动并重建了我的可移植库,还卸载了VS的IronPython和Python工具,将其替换为2.7.3和1.5版本(VS2010).仍然出现"System.Core,Version = 2.0.5.0"错误.

Update: I restarted and rebuilt my portable library after the MS KB2468871 update, and also uninstalled IronPython and Python tools for VS, replacing them by versions 2.7.3 and 1.5 (VS2010). The error with 'System.Core, Version=2.0.5.0' still occurs.

推荐答案

FileNotFoundException表示某些东西正在使用Assembly.LoadFile而不是Assembly.LoadFrom加载程序集,但未正确处理程序集策略.我不确定Visual Studio中的Python代码如何工作,但是如果您能够在加载可移植程序集之前运行任何引导程序代码,请尝试在此显示的代码: PCL可重定向程序集未在MS CRM插件内部重定向.

The FileNotFoundException is an indication that something is loading the assemblies using Assembly.LoadFile instead of Assembly.LoadFrom, but not handling assembly policy correctly. I'm not sure how Python code in Visual Studio works, but if you are able to run any bootstrapper code before the portable assembly has been loaded, try the code that I showed here: PCL Retargetable Assembly not redirected inside MS CRM Plugin.

这篇关于如何在Iron Python脚本中加载可移植的.NET库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆