简体   繁体   中英

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.

.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).

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

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).

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)

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.

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.

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?

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.

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 .

use sys.path to add pathes to where your .net dlls are:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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