如何在IronPython中创建.NET程序集并从C#中调用它? [英] How do I create a .NET assembly in IronPython and call it from C#?

查看:118
本文介绍了如何在IronPython中创建.NET程序集并从C#中调用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  1. 我没有问如何从IronPython调用C#.最容易找到的文档描述了如何从IronPython内部调用C#. (例如,IronPython附带的教程.)我想做相反的事情,从C#调用IronPython.

  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解释器.这很有用,但是我对从IronPython创建编译的程序集更感兴趣.也就是说,我想对IronPython程序集进行方法调用,而不是将源代码字符串传递给解释器.

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.

创建完程序集后,调用该程序集有哪些技巧? 博客文章我发现说:

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

...从C#调用Python程序集 是不平凡的.那个python程序集 包含动态类型,但不是 轻松反映到有用的C#中 对象.

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

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

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

更新:我目前最感兴趣的方案是将两个或三个double值传递到Python中,然后取回一个或两个double值.如果我可以传入一个字符串然后取回一个字符串,那将是非常了不起的,但是我的首要任务是来回传递数字.

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.

推荐答案

如果您的目标是创建一个包含类似于C#创建的类型的类型的程序集,则这实际上是不可能的.这里的主要问题是Python类型与CLR类型的工作方式大不相同.一方面,它们可以在运行时进行更改,而CLR类型是完全静态的.因此,今天要实现这一目标的唯一方法是使用托管接口来创建一个小的C#存根,将工作委托给Python代码.

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.

IronPython确实具有编译成程序集的能力(这是博客文章所指的),但是我们这样做的主要目的是使您可以将IronPython应用程序部署给客户,而不必提供给他们源代码.

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.

可以通过在此答案中添加评论来随时跟进更具体的情况,我将很乐意为您提供实施建议.

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.

这篇关于如何在IronPython中创建.NET程序集并从C#中调用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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