有没有办法从C#代码中使用IronPython对象和函数(编译成程序集)?

时间:2021-11-03 03:14:39

IronPython.net documentation says the MSIL in the assembly isn't CLS-compliant, but is there a workaround?

IronPython.net文档说组件中的MSIL不符合CLS,但是有解决方法吗?

2 个解决方案

#1


1  

This was partly a motivation for adding the dynamic type to C# 4.0. The biggest problem is that IronPython declarations doesn't include type information, which makes it difficult to use it from C#. The dynamic keyword adds support for such dynamically typed objects to C# 4.0. See for example:

这部分是将动态类型添加到C#4.0的动机。最大的问题是IronPython声明不包含类型信息,因此很难从C#中使用它。 dynamic关键字将对此类动态类型对象的支持添加到C#4.0。参见例如:

Calling functions/objects from C# 3.0 is a bit more annoying, but it is still possible. You'll just have to write something like foo.Invoke("Bar", 42) instead of just writing foo.Bar(42).

从C#3.0调用函数/对象有点烦人,但它仍然有可能。你只需要写一些像foo.Invoke(“Bar”,42)的东西,而不只是写foo.Bar(42)。

#2


0  

I'm typing this on my phone so please forgive any silly mistakes. To use the compiled assembly, make sure you compile with clr.CompileModules, NOT pyc.py. Then in your C# call the LoadAssembly method on your Python ScriptEngine object. The module can then be imported by calling the ImportModule method on your ScriptEngine. From there if you can take advantage of the dynamic keyword, do so. Otherwise you'll be stuck with some magic string heavy calls to GetVariable. Also note that you'll have to provide the standard library to your compiled Python Assembly in one form or another.

我在手机上输入这个,所以请原谅任何愚蠢的错误。要使用已编译的程序集,请确保使用clr.CompileModules编译,而不是pyc.py.然后在C#中调用Python ScriptEngine对象上的LoadAssembly方法。然后可以通过调用ScriptEngine上的ImportModule方法导入该模块。如果您可以利用动态关键字,请执行此操作。否则你会遇到一些魔法串重的GetVariable调用。另请注意,您必须以一种或另一种形式为编译的Python程序集提供标准库。

#1


1  

This was partly a motivation for adding the dynamic type to C# 4.0. The biggest problem is that IronPython declarations doesn't include type information, which makes it difficult to use it from C#. The dynamic keyword adds support for such dynamically typed objects to C# 4.0. See for example:

这部分是将动态类型添加到C#4.0的动机。最大的问题是IronPython声明不包含类型信息,因此很难从C#中使用它。 dynamic关键字将对此类动态类型对象的支持添加到C#4.0。参见例如:

Calling functions/objects from C# 3.0 is a bit more annoying, but it is still possible. You'll just have to write something like foo.Invoke("Bar", 42) instead of just writing foo.Bar(42).

从C#3.0调用函数/对象有点烦人,但它仍然有可能。你只需要写一些像foo.Invoke(“Bar”,42)的东西,而不只是写foo.Bar(42)。

#2


0  

I'm typing this on my phone so please forgive any silly mistakes. To use the compiled assembly, make sure you compile with clr.CompileModules, NOT pyc.py. Then in your C# call the LoadAssembly method on your Python ScriptEngine object. The module can then be imported by calling the ImportModule method on your ScriptEngine. From there if you can take advantage of the dynamic keyword, do so. Otherwise you'll be stuck with some magic string heavy calls to GetVariable. Also note that you'll have to provide the standard library to your compiled Python Assembly in one form or another.

我在手机上输入这个,所以请原谅任何愚蠢的错误。要使用已编译的程序集,请确保使用clr.CompileModules编译,而不是pyc.py.然后在C#中调用Python ScriptEngine对象上的LoadAssembly方法。然后可以通过调用ScriptEngine上的ImportModule方法导入该模块。如果您可以利用动态关键字,请执行此操作。否则你会遇到一些魔法串重的GetVariable调用。另请注意,您必须以一种或另一种形式为编译的Python程序集提供标准库。