简体   繁体   English

如何在Iron Python脚本中加载可移植的.NET库?

[英]How to load portable .NET library within Iron Python script?

I have serious troubles loading a portable .NET library (to be used in standard .NET and Silverlight environment) from a Python script. 从Python脚本加载可移植的.NET库(在标准的.NET和Silverlight环境中使用)时遇到了严重的麻烦。

.NET DLL file version is 4.0.3.319.233 (System.Core.DLL), IronPython is 2.7.1, running in 32bit/x86 mode. .NET DLL文件版本为4.0.3.319.233(System.Core.DLL),IronPython为2.7.1,以32bit / x86模式运行。 Visual Studio 2010 with C# under .NET 4. Microsoft .NET update KB2468871 for Portable library usage is also installed (Version 2). 在.NET 4下使用C#的Visual Studio 2010 4.还安装了用于可移植库使用的Microsoft .NET更新KB2468871(版本2)。

If I try to load the library from the Python script: 如果我尝试从Python脚本加载库:

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

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). 无法访问它,当脚本到达某个类型时,它会说: 'namespace#'的属性[可移植程序集中的类型]是只读的“表示程序集尚未加载(或者作为Silverlight,并且不能被Python脚本使用)。

Changing the code to: (Assembly class from System.Reflection) 将代码更改为:(来自System.Reflection的程序集类)

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

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. 导致错误: exceptions.IOError出现消息:[Errno 2]无法加载文件或程序集'System.Core,Version = 2.0.5.0,Culture = neutral,PublicKeyToken = 7cec85d7bea7798e,Retargetable = Yes'或其依赖项之一。 The system cannot find the file specified. 该系统找不到指定的文件。

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. 最后一个代码似乎有效,当从另一个.NET程序中自动调用Python脚本,实例化它自己的Python引擎时,但是当从Visual Studio中的Python项目执行脚本时会出现上述错误。 Python settings in VisualStudio, Tools\\Options\\Python Tools\\Interpreter Options are for x86/32bit mode. VisualStudio中的Python设置,Tools \\ Options \\ Python Tools \\ Interpreter选项适用于x86 / 32位模式。 All environment parameters show that .NET 4 is used. 所有环境参数都显示使用了.NET 4。

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? 我现在有多种方法可以从C#/ .NET生成的Python引擎中修复它,但是如何在基本的IronPython运行时环境中加载可移植组件,以便它在正确的.NET 4环境中工作,而不是尝试加载任何.NET 2的东西?

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). 更新:我在MS KB2468871更新后重新启动并重建了我的可移植库,并且还为VS卸载了IronPython和Python工具,将其替换为版本2.7.3和1.5(VS2010)。 The error with 'System.Core, Version=2.0.5.0' still occurs. “System.Core,Version = 2.0.5.0”的错误仍然存​​在。

The FileNotFoundException is an indication that something is loading the assemblies using Assembly.LoadFile instead of Assembly.LoadFrom, but not handling assembly policy correctly. FileNotFoundException指示某些内容正在使用Assembly.LoadFile而不是Assembly.LoadFrom加载程序集,但未正确处理程序集策略。 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 . 我不确定Visual Studio中的Python代码是如何工作的,但如果您在加载可移植程序集之前能够运行任何引导程序代码,请尝试我在此处显示的代码: PCL Retargetable Assembly未在MS CRM插件中重定向

use sys.path to add pathes to where your .net dlls are: 使用sys.path将pathes添加到.net dll所在的位置:

import sys
sys.path.append("c:\MyDotNetDir");

import clr
clr.AddReference("MyDotNetAssembly.dll")

# do not forget to import the namespace

import Erik.MyDotNetAssemblyNamespace

inst = Erik.MyDotNetAssemblyNamespace.MyDotNetObject()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM