一,python
安装ironpython
新建控制台程序,引入
IronPython,Microsoft.Scripting
新建xxx.py文件
var u = new User() { Name = "test" };var engine = IronPython.Hosting.Python.CreateEngine(); var scope = engine.CreateScope();var source = engine.CreateScriptSourceFromFile("python/xxx.py");source.Execute(scope);var fun = scope.GetVariable("welcome");Console.WriteLine(fun(u));
加载了一个py文件,并且从中获取welcome这个方法,最后调用这个方法
传递的参数是一个c#类,需要在py中读取这个类的属性
import clrclr.AddReference('cslib')from cslib import *def welcome(u): return "hello " + u.Name
(ns test.add) (defn add [a b] (+ a b))
RT.load("add");var output = RT.var("test.add", "add");var x= Convert.ToInt32(output.invoke(1, 2));Console.WriteLine(x);
load对应文件名,var两个参数对应ns和方法名
向clj传递c#对象
var hello = RT.var("test.add", "hello");hello.invoke(u);
在clj里引入
(System.Reflection.Assembly/LoadWithPartialName "cslib")
访问属性 使用 (.属性名 实例)
(defn hello [u] (print (.Name u)))
参考
两个都需要设置