首先我们需要一个简单的pythoncode:
class HelloWorld:
def __init__(self):
pass
def hello(name):
return 'hello:'+name
It's easy to figure out It's function, right?
然后我们创建一个VS工程:
-
创建工程。
-
添加.net引用:IronPython引用和Microsoft.Scripting引用,这里有很多Version,选择需要的,否则不能用的。(装了Ironpython你就可以找到他们了)
-
创建运行时。
-
绑定动态变量。
-
You can use it freely.
Coding below:
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
ScriptRuntime runtime=Python.CreatRuntime();
dynamic obj=runtime.UseFile('helloworld.py');
dynamic cls=obj.HelloWorld();
Console.Writeline(cls.hello('Adair'))