简体   繁体   English

如何在IronPython中调用C#/ .NET命名空间?

[英]How to invoke C#/.NET namespace in IronPython?

I'm looking to replicate the following in IronPython and searching has so far been fruitless and/or disappointing. 我想在IronPython中复制以下内容,到目前为止搜索一直没有结果和/或令人失望。

namespace Groceries
{
     public class ChocolateMilk : Milk
     {
          // Other stuff here
     }
}

The idea would be that the compiled Python DLL will be loaded into a C# program through System.Reflection.Assembly.Load and a GetType("Groceries.ChocolateMilk") on the loaded DLL would not return null. 想法是编译的Python DLL将通过System.Reflection.Assembly.Load加载到C#程序中,并且加载的DLL上的GetType(“Groceries.ChocolateMilk”)不会返回null。

The most recent answer I was able to find was in 2008 and said that it was impossible without using the Hosting API - http://lists.ironpython.com/pipermail/users-ironpython.com/2008-October/008684.html . 我能找到的最新答案是在2008年,并表示如果不使用Hosting API,这是不可能的 - http://lists.ironpython.com/pipermail/users-ironpython.com/2008-October/008684.html

Any suggestions on how to accomplish this would be greatly appreciated. 任何关于如何实现这一点的建议将不胜感激。 Any conclusions that this is currently impossible to do via IronPython will also be appreciated, but less so. 目前通过IronPython无法做出的任何结论也将受到赞赏,但不那么重要。

I'm a bit confused on what you're asking here. 我对你在这里问的问题有点困惑。 Are you trying to instantiate that C# code in your IronPython modules? 您是否尝试在IronPython模块中实例化C#代码? Or do you have the equivalent classes written in IronPython and you want to instantiate them in your C# code? 或者您是否有使用IronPython编写的等效类,并且您希望在C#代码中实例化它们?

Based on the link you posted, I suppose you're going for the latter and have IronPython classes that you want instantiated in your C# code. 根据您发布的链接,我想您将选择后者,并且您希望在C#代码中实例化IronPython类。 The answer is, you cannot directly instantiate them. 答案是,你无法直接实例化它们。 When you compile IronPython code to an assembly, you cannot use the types defined there with your regular .NET code since there is not a one-to-one mapping between IronPython classes and .NET classes. 将IronPython代码编译为程序集时,不能使用常规.NET代码中定义的类型,因为IronPython类和.NET类之间没有一对一的映射。 You would have to host the assembly in your C# project and instantiate it that way. 您必须在C#项目中托管程序集并以这种方式实例化它。

Consider this module, Groceries.py compiled to Groceries.dll residing in the working directory: 考虑这个模块, Groceries.py编译为驻留在工作目录中的Groceries.dll

class Milk(object):
    def __repr__(self):
        return 'Milk()'

class ChocolateMilk(Milk):
    def __repr__(self):
        return 'ChocolateMilk()'

To host the module in your C# code: 要在C#代码中托管模块:

using System;

using IronPython.Hosting;
using System.IO;
using System.Reflection;

class Program
{
    static void Main(string[] args)
    {
        var engine = Python.CreateEngine();
        var groceriesPath = Path.GetFullPath(@"Groceries.dll");
        var groceriesAsm = Assembly.LoadFile(groceriesPath);
        engine.Runtime.LoadAssembly(groceriesAsm);

        dynamic groceries = engine.ImportModule("Groceries");
        dynamic milk = groceries.ChocolateMilk();
        Console.WriteLine(milk.__repr__()); // "ChocolateMilk()"
    }
}

Otherwise to go the other way and create an instance of your .NET type in your IronPython code (as your title suggests). 否则以另一种方式在IronPython代码中创建.NET类型的实例(如标题所示)。 You'd need to add the path to your assembly, reference it, then you could instantiate it as needed. 您需要添加程序集的路径,引用它,然后您可以根据需要实例化它。

# add to path
import sys
sys.path.append(r'C:\path\to\assembly\dir')

# reference the assembly
import clr
clr.AddReferenceToFile(r'Groceries.dll')

from Groceries import *
chocolate = ChocolateMilk()
print(chocolate)

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

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