IronPython.net documentation says the MSIL in the assembly isn't CLS-compliant, but is there a workaround?
views:
62answers:
2Is there a way to use IronPython objects and functions (compiled into an assembly) from C# code?
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:
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)
.
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.