Silverlight下面的IronPyton(1)

it2025-02-19  6

  学习背景:

         项目中需要一种脚本,可以与静态语言进行交互,比如引入外部的dll,实例化一个对象,修改页面上控件的属性等

  翻阅了很多资料,测试了几种脚本,c#-script,天使坠同志写的RoughScript 1.3版,JavaScript,Jscript,IronPython,IronRuby,最终认为IronPython,IronRuby可以满足项目的基本要求。

  下面看看silverlight下面IronPython基本的应用:

          1:首先了解一下DLR

           

     这个图片属于转载,大家可以看看了解一下基本运行过程    

ScriptRuntime: 创建 DLR 运行环境,这是整个执行过程的起始点,它表示一个全局的执行状态(比如程序集引用等等)。每个应用程序域(AppDomain)中可以启动多个 ScriptRuntime。ScriptScope: 构建一个执行上下文,其中保存了环境及全局变量。宿主(Host)可以通过创建不同的 ScriptScope 来提供多个数据隔离的执行上下文。ScriptEngine: DLR 动态语言(比如 IronPython) 执行类,可于解析和执行动态语言代码。ScriptSource: 操控动态语言代码的类型,我们可以编译(Compile)、读取(Read Code Lines)或运行(Execute)代码。CompiledCode: 调用 ScriptSource.Compile() 将源代码编译成 CompiledCode,这样多次执行就无需重复编译,从而提高执行性能。ObjectOperations: 提供了相关方法,允许我们在宿主(Host)中操作 DLR 对象成员(Member)。

    我自己所做的简单的测试代码如下 :

            

namespace SilverlightApplication1{    public partial class MainPage : UserControl    {        public MainPage()        {            InitializeComponent();            ScriptEngine engine = Python.CreateEngine();            ScriptScope scope = engine.CreateScope();            #region demo1   字条串的输出

            //var strExpression = "\"Hello:\" + str";            //var sourceCode = engine.CreateScriptSourceFromString(strExpression);   //脚本是一个字条串            //scope.SetVariable("str", "Python");  //注册一个脚本变量,可以参与运算            //var actual = sourceCode.Execute<string>(scope);//执行脚本            //scope.RemoveVariable("str");//移除脚本变量

            //txt.Text = actual;

            #endregion

            #region demo2  在脚本中定义一个函数            //这里需要注意def MyFunction(n):前不能有空格,return 2*n 必须有空格            /*  var strExpression = @"def MyFunction(n): return 2*n";              var sourceCode = engine.CreateScriptSourceFromString(strExpression).Compile().Execute(scope); 

              var func = engine.GetVariable<Func<int, int>>(scope, "MyFunction");

             txt.Text =func(3).ToString();*/

            #endregion

            #region demo3              /*   var strExpression = "CMethod('python,tiger')";            var sourceCode = engine.CreateScriptSourceFromString(strExpression);            scope.SetVariable("CMethod", (Func<string, string>)TMethod);            var actual = sourceCode.Execute<string>(scope);            scope.RemoveVariable("CMethod");            txt.Text = actual;*/

            #endregion

            #region demo4           /* Test test = new Test();            var strExpression = @"test.Hello()";            var sourceCode = engine.CreateScriptSourceFromString(strExpression);            scope.SetVariable("test", test);            var actual = sourceCode.Execute<string>(scope);            txt.Text = actual;*/            #endregion

        /*    #region demo5   在脚本中导入dll,测试失败,只能在外部文件中导入命名空间,以调用文件的方式执行,以语句的方式执行失败            var strExpression = @"import clrclr.AddReference('System.Windows.Controls') from System.Windows.Controls import * tt=Button()tt.Content='dasf'MessageBox.Show(tt.Content.ToString())";            var sourceCode = engine.CreateScriptSourceFromString(strExpression);            sourceCode.Execute(scope);            //txt.Text = actual;           

            #endregion*/        }        public string TMethod(string info)        {            return "Hello:" + info;        }

    }

    public class Test    {        public string Hello()        {            return "Hello: tiger ,are you ok";        }    }}

namespace IronPythonTest{    public class Test    {        public string Hello()        {            return "Hello World";        }    }}

 

以上是自己根据网上查询的资料自己做的测试,初步简单的在silverlight中调用了IronPtyhon

 下面看看在silverlight中如何调用.py文件

转载于:https://www.cnblogs.com/wangxh/archive/2010/10/24/IronPython.html

最新回复(0)