如何在IronPython中创建.NET程序集并从C#调用它?

时间:2021-08-24 06:29:57

I want to create an assembly using IronPython can call it from C#. Here are two things I am not asking.

我想使用IronPython创建一个程序集,可以从C#中调用它。这是我不问的两件事。

  1. I'm not asking how to call C# from IronPython. The easiest documentation to find describes how to call C# from inside IronPython. (For example, the tutorial that ships with IronPython.) I want to do the opposite, call IronPython from C#.

    我不是问如何从IronPython调用C#。最容易找到的文档描述了如何从IronPython中调用C#。 (例如,IronPython附带的教程。)我想反过来,从C#调用IronPython。

  2. I'm not asking how to embed the IronPython interpreter. I've found several useful references (e.g. here and here) on how to call the IronPython interpreter from C#. That's helpful, but I'm more interested in creating a compiled assembly from IronPython. That is, I'd like to make method calls into the IronPython assembly rather than passing source code strings to the interpreter.

    我不是问如何嵌入IronPython解释器。我已经找到了几个有用的参考资料(例如这里和这里),介绍如何从C#调用IronPython解释器。这很有帮助,但我更感兴趣的是从IronPython创建一个已编译的程序集。也就是说,我想对IronPython程序集进行方法调用,而不是将源代码字符串传递给解释器。

Once I've created the assembly, what are some tips on calling into it? One blog post I found said:

一旦我创建了程序集,有什么关于调用它的提示?我发现一篇博文说:

... calling a Python assembly from C# is non-trivial. That python assembly contains dynamic types, which are not easily reflected into useful C# objects.

...从C#调用Python程序集是非常重要的。该python程序集包含动态类型,这些类型不容易反映到有用的C#对象中。

Do you know any sort of cheat sheet for passing basic data types between IronPython and C#?

你知道在IronPython和C#之间传递基本数据类型的任何类型的备忘单吗?

Update: The scenario I am most interested in for now is passing two or three double values into Python and getting one or two double values back. If I could pass in a string and get a string back that would be terrific, but my first priority is just passing numbers back and forth.

更新:我现在最感兴趣的场景是将两个或三个double值传递给Python并返回一个或两个double值。如果我可以传入一个字符串并获得一个非常棒的字符串,但我的首要任务就是来回传递数字。

1 个解决方案

#1


5  

This isn't really possible if your goal is to create an assembly which contains types that look like the types created by C#. The main problem here is that Python types work considerably differently than CLR types. For one thing, they can be mutated at runtime while CLR types are totally static. So the only way to achieve this today is to use the hosting interfaces to create a small C# stub which delegates the work to Python code.

如果你的目标是创建一个包含看起来像C#创建的类型的类型的程序集,那么这实际上是不可能的。这里的主要问题是Python类型与CLR类型的工作方式大不相同。首先,它们可以在运行时进行变异,而CLR类型是完全静态的。所以今天实现这一目标的唯一方法是使用托管接口创建一个小的C#存根,它将工作委托给Python代码。

IronPython does have the ability to compile to an assembly -- which is what that blog post refers to -- but we did this primarily so that you could deploy an IronPython application to your customers without having to give them the source code.

IronPython确实能够编译成程序集 - 这就是博客文章所指的内容 - 但我们这样做主要是为了让您可以将IronPython应用程序部署到客户而无需向他们提供源代码。

Feel free to follow up with a more specific scenario by adding a comment to this answer, and I'll be happy to provide advice on how to implement.

通过在此答案中添加注释,随意跟进更具体的场景,我很乐意提供有关如何实施的建议。

#1


5  

This isn't really possible if your goal is to create an assembly which contains types that look like the types created by C#. The main problem here is that Python types work considerably differently than CLR types. For one thing, they can be mutated at runtime while CLR types are totally static. So the only way to achieve this today is to use the hosting interfaces to create a small C# stub which delegates the work to Python code.

如果你的目标是创建一个包含看起来像C#创建的类型的类型的程序集,那么这实际上是不可能的。这里的主要问题是Python类型与CLR类型的工作方式大不相同。首先,它们可以在运行时进行变异,而CLR类型是完全静态的。所以今天实现这一目标的唯一方法是使用托管接口创建一个小的C#存根,它将工作委托给Python代码。

IronPython does have the ability to compile to an assembly -- which is what that blog post refers to -- but we did this primarily so that you could deploy an IronPython application to your customers without having to give them the source code.

IronPython确实能够编译成程序集 - 这就是博客文章所指的内容 - 但我们这样做主要是为了让您可以将IronPython应用程序部署到客户而无需向他们提供源代码。

Feel free to follow up with a more specific scenario by adding a comment to this answer, and I'll be happy to provide advice on how to implement.

通过在此答案中添加注释,随意跟进更具体的场景,我很乐意提供有关如何实施的建议。