IronPython与Ajax(转)
IronPython与Ajax
整合总是让人兴奋的,过程总是让人充满期待的,Javascript 拥有了调用后台的扩展,Aspx文件也可以用Python混合编写。
用的IronPython是1.1 的 2.0的还是 A4 版本貌似不怎么可靠。
这只是初级模型,后期还好慢慢的修改这里只是提供了一个思路而已。
需要解决的问题,IronPython代码结果的传输,out ref 参数毫无用处,控制台输出也是令人烦恼的,如何完整的传出一个对象,进行C#的交互工作呢? 这里想到了可爱的委托,对的 非常正确,IronPython支持委托,虽然也许不怎么完美,但是足够了。
首先在嵌入类添加了一些个委托,(以下代码都只是片断,如有需要完整代码请Mail索要)
public delegate object ResultRegular();
public event ResultRegular OnResult;
/// 运行函数
/// </summary>
/// <param name="code">The code.</param>
/// <param name="dllpath">The dllpath.</param>
/// <returns></returns>
protected object Run(string code, string dllpath)
{
script.RunScript(code, dllpath);
if (OnResult != null)
{
object res = OnResult();//返回结果对象
OnResult = null;
return res;
}
return script.Ouputmsg;
}
如果能重写IronPython的return 的话 当然也许有更好的结果。
以前我尝试过拿一个对象保存结果,通过python进行输出后,使用一个key进行处理,不过涉及到对象周期以及key封装处理的问题。。
令人头疼的玩艺在以上代码中解决了。在Python中我们需要做的就是创建一个可以轻便使用的东西。
python代码
def cprint(key,value):
root.Add(key,value)
print(value)
return
root.Add(key,value)
print(value)
return
class PReturn:
def OnReturn(self,obj):
self._obj=obj
root.OnResult+=tager
return obj
def tager():
return _obj
def OnReturn(self,obj):
self._obj=obj
root.OnResult+=tager
return obj
def tager():
return _obj
OnReturnA = PReturn()
OnReturn=OnReturnA.OnReturn
OnReturn=OnReturnA.OnReturn
root 令人困惑么?
this.Globals(this, "root");
只是指向了这个委托而已。
使用:
def test():
OnReturn('123')
test()
什么?貌似能传递对象? 嗯 非常正确,可以传递一切你想要的……
浙公网安备 33010602011771号