包含DLR ScriptRuntime

DLR是微软的一个开源项目.为.NET影城程序提供了动态脚本注入支持.DLR构建的功能包含两个方面,一个是共享的动态类型系统,一个是标准的承载模型.但是VS并没有继承相关的DLL,大家可以从Codeplex获得源码.这里不得不提及CLR,它是整个运行环境的基础,DLR也是运行在其上,这样是由显而易见的好处的,CLR的垃圾垃圾回收,JIT编译,安全模式,DLR也能享用这些底层架构功能,如果我们对垃圾回收进行优化,或者是提供某种功能,那么DLR相应的也能享用这种便利.DLR内部为了提高执行效率,会让自己先编译脚本,然后缓存.这有些类似JIT机制.避免重复加载,解析外部脚本.

在实际业务逻辑中,我们希望能够实时动态执行存储在文件中的代码或者完整一个业务逻辑功能,甚至我们可以动态选择脚本语言引擎,在应用程序域中动态生成脚本,并注入脚本,来达到控制业务逻辑的目的.

ScriptRuntime: 创建 DLR 运行环境,这是整个执行过程的起始点,它表示一个全局的执行状态(比如程序集引用等等)。每个应用程序域(AppDomain)中可以启动多个 ScriptRuntime。

ScriptScope: 构建一个执行上下文,其中保存了环境及全局变量。宿主(Host)可以通过创建不同的 ScriptScope 来提供多个数据隔离的执行上下文。

ScriptEngine: DLR 动态语言(比如 IronPython) 执行类,可于解析和执行动态语言代码。

ScriptSource: 操控动态语言代码的类型,我们可以编译(Compile)、读取(Read Code Lines)或运行(Execute)代码。

CompiledCode: 调用 ScriptSource.Compile() 将源代码编译成 CompiledCode,这样多次执行就无需重复编译,从而提高执行性能。

ObjectOperations: 提供了相关方法,允许我们在宿主(Host)中操作 DLR 对象成员(Member)。

现在我们来构建脚本引擎

应用程序执行脚本的方法有很多种:

1.通过ScriptRuntime.ExcuteFile获得ScriptScope

2.通过ScriptRuntime.UseFile获得ScriptScope

3.通过ScriptRuntime.CreateScope获得ScriptScope,再执行Script>execute或ScriptScope.IncludeFile

4.通过ScriptRuntime.GetEngine或ScriptRuntime.GetEngineByFileName获得ScriptEngine.调用ScriptEngine的CreateScriptSourceFromStrng或CreateScriptSourceFromFile或CreateScriptSourceFromStream创建ScriptSource,再执行ScriptSource.Execute

5.有了ScriptSource也不一定要Execute,可以先调用Compile,产生CompiledCode,最后调用CompiledCOde.Execute.

说了这么多,你肯定觉得每个例子,说个JB,说不定又觉得我在忽悠你,我是那样的人吗?

下面看看JB,不对,是案例:

第一步,安装:我是用的是VS2012,我从http://ironpython.codeplex.com/releases/view/12482下载的是IronPython-2.6.msi(不大,7M作用),直接安装就可以了.使用默认的安装路径就可以了,C:\Program Files(86)\IronPython 2.6(这是我的默认路径)

第二步:引入相应的dll

看到引用了吗?右键单击,选择添加引用,将C:\Program Files(86)\IronPython 2.6的IronPython.dll,Microsoft.Scripting.Core.dll,Microsoft.Scripting.dll三个dll引入进来.

第三步:应用

C:\Program Files\IronPython 2.6\Tutorial\Tutorial.htm是IronPython的应用指导,写的很仔细。ipy.exe是IronPython 的运行控制台,如果你想学习IronPython 的语法可以使用这个工具。IronPython 的语法这里就不详细介绍了,如果想进一步学习,可以下载IronPython in Action。

看代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Reflection;

using System.Text;

using System.Threading.Tasks;

//下面这二个命名空间必须要引入

using Microsoft.Scripting.Hosting;

using IronPython.Hosting;

namespace ConsoleApplication20

{

class Program

{

static void Main(string[] args)

{

/*因为本人没学过Python,所以不会,但是我学过shell和JS,感觉语法差不多,大同小异*/

ScriptEngine engine = Python.CreateEngine();

ScriptScope scope = engine.CreateScope();

var strExpression = "1+2";

var sourceCode = engine.CreateScriptSourceFromString(strExpression);

var actual = sourceCode.Execute<int>();

Console.WriteLine(actual);

Console.ReadKey();

}

}

}

这段代码是用来计算1+2的结果.

ScriptEngine和ScriptScope是在.net中使用IronPython脚本的两个基础类,ScriptSource是运行IronPython的基础类,这里sourceCode就是一个ScriptSource.

有时我们希望给IronPython代码中传入一个变量值:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Reflection;

using System.Text;

using System.Threading.Tasks;

//下面这二个命名空间必须要引入

using Microsoft.Scripting.Hosting;

using IronPython.Hosting;

namespace ConsoleApplication20

{

class Program

{

static void Main(string[] args)

{

ScriptEngine engine = Python.CreateEngine();

ScriptScope scope = engine.CreateScope();

var strExpression = "\"hello:\" + str ";

var sourceCode = engine.CreateScriptSourceFromString(strExpression);

scope.SetVariable("str","python");

var actual=sourceCode.Execute<string>(scope);

scope.RemoveVariable("str");

Console.WriteLine(actual);

Console.ReadKey();

}

}

}

C#调用IronPython函数

ScriptEngine engine = Python.CreateEngine();

ScriptScope scope = engine.CreateScope();

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");

Console.WriteLine(func(3));

Console.ReadKey();

这里需要注意def MyFunction(n):前面不能有空格,return 2*n必须有空格

IronPython调用C#函数

using System;

using System.Collections.Generic;

using System.Linq;

using System.Reflection;

using System.Text;

using System.Threading.Tasks;

//下面这二个命名空间必须要引入

using Microsoft.Scripting.Hosting;

using IronPython.Hosting;

namespace ConsoleApplication20

{

class Program

{

static void Main(string[] args)

{

ScriptEngine engine = Python.CreateEngine();

ScriptScope scope = engine.CreateScope();

var strExpression = "CMethod('Python')";

var sourceCode = engine.CreateScriptSourceFromString(strExpression);

scope.SetVariable("CMethod", (Func<string, string>)TMethod);

var actual = sourceCode.Execute<string>(scope);

scope.RemoveVariable("CMethod");

Console.WriteLine(actual);

Console.ReadKey();

}

public static string TMethod(string info)

{

return "Hello:" + info;

}

}

}

案例:如果需要使用某个对象中的某个函数

using System;

using System.Collections.Generic;

using System.Linq;

using System.Reflection;

using System.Text;

using System.Threading.Tasks;

//下面这二个命名空间必须要引入

using Microsoft.Scripting.Hosting;

using IronPython.Hosting;

namespace ConsoleApplication20

{

class Program

{

static void Main(string[] args)

{

ScriptEngine engine = Python.CreateEngine();

ScriptScope scope = engine.CreateScope();

Test test = new Test();

var strExpression = @"test.Hello()

";

var sourceCode = engine.CreateScriptSourceFromString(strExpression);

scope.SetVariable("test", test);

var actual = sourceCode.Execute<string>(scope);

Console.WriteLine(actual);

Console.ReadKey();

}

}

public class Test

{

public void Hello()

{

Console.WriteLine("hello ,world");

}

}

}

案例:如果需要在IronPython实例化使用某个对象,就稍微复杂一点:

using IronPython.Hosting;

using Microsoft.Scripting.Hosting;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace IronPythonTest

{

class Program

{

static void Main(string[] args)

{

ScriptEngine engine = Python.CreateEngine();

ScriptScope scope = engine.CreateScope();

var strExpression = @"

import clr, sys

clr.AddReference('IronPythonTest')

from IronPythonTest import *

test=Test()

test.Hello()

";

var sourceCode = engine.CreateScriptSourceFromString(strExpression);

var actual = sourceCode.Execute<string>(scope);

Console.WriteLine(actual);

Console.ReadKey();

}

}

public class Test

{

public string Hello()

{

return "Hello World";

}

}

}

C#编程(七十一)----------DLR ScriptRuntime的更多相关文章

  1. 孤荷凌寒自学python第七十一天开始写Python的第一个爬虫

    孤荷凌寒自学python第七十一天开始写Python的第一个爬虫 (完整学习过程屏幕记录视频地址在文末) 在了解了requests模块和BeautifulSoup模块后,今天开始真正写一个自己的爬虫代 ...

  2. 第三百七十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现我的搜索以及热门搜索

    第三百七十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)用Django实现我的搜索以及热门 我的搜素简单实现原理我们可以用js来实现,首先用js获取到 ...

  3. 深度学习(七十一)darknet 源码阅读

    深度学习(七十一)darknet 源码阅读

  4. “全栈2019”Java第七十一章:外部类访问静态内部类成员详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  5. 《手把手教你》系列技巧篇(七十一)-java+ selenium自动化测试-自定义类解决元素同步问题(详解教程)

    1.简介 前面宏哥介绍了几种关于时间等待的方法,也提到了,在实际自动化测试脚本开发过程,百分之90的报错是和元素因为时间不同步而发生报错.本文介绍如何新建一个自定义的类库来解决这个元素同步问题.这样, ...

  6. C#编程(七十一)---------- 自定义特性

    自定义特性 在说自定义之前,有必要先介绍一些基本的概念. 元数据:就是C#中封装的一些类,无法修改,类成员的特性被称为元数据中的注释 1.什么是特性? (1)属性和特性的区别 属性:属性是面向对象思想 ...

  7. 学习ASP.NET Core Razor 编程系列十一——把新字段更新到数据库

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  8. Linux 网络编程七(非阻塞socket:epoll--select)

    阻塞socket --阻塞调用是指调用结果返回之前,当前线程会被挂起.函数只有在得到结果之后才会返回. --对于文件操作 read,fread函数调用会将线程阻塞(平常使用read感觉不出来阻塞, 因 ...

  9. TCP编程(七)

    此为网络编程的一个系列,后续会把内容补上...

随机推荐

  1. Android 6.0 变更

    Android 6.0(API 级别 23)除了提供诸多新特性和功能外,还对系统和 API 行为做出了各种变更.本文重点介绍您应该了解并在开发应用时加以考虑的一些主要变更. 如果您之前发布过 Andr ...

  2. C# wpf 阻止*和|的输入

    private void texBox_KeyDown(object sender, KeyEventArgs e) { if (Keyboard.Modifiers == ModifierKeys. ...

  3. (一)Dubbo简介

    Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成.具体可以看 百度百科 https://baike.ba ...

  4. Tomcat启动默认访问项目

    一般有两种可以实现:推荐使用这一种.更灵活 一般项目的编译项目都在Tomcat的webapps下,项目的访问路径一般为:http://localhost:8080/项目虚拟路径.但是Tomcat的默认 ...

  5. Scala 学习笔记(1)之入门篇

    Scala is pronounced skah-lah. Scala 全称为 scalable language,是一种面向对象(object)- 函数式(functional)静态类型(stati ...

  6. net mvc中angular

    把angular项目整合到.net mvc中   之前的开发选择的是完全舍弃服务端,仅保留最简单web服务器提供angular经打包的静态资源,此外所有的业务与数据请求都访问一个分离的WebApi来实 ...

  7. C++ map.insert: pair和make_pair区别

    C++ map.insert: pair和make_pair区别 \*********************************\ map<uint32_t, string> tem ...

  8. plsql oracle 使用教程

    课程 一 PL/SQL 基本查询与排序 本课重点: 1.写SELECT语句进行数据库查询 2.进行数学运算 3.处理空值 4.使用别名ALIASES 5.连接列 6.在SQL PLUS中编辑缓冲,修改 ...

  9. 005.MySQL双主-Master01可用配置

    [root@Master01 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_de ...

  10. Linux文件的所有权与权限

    要了解Linux的权限,需要和Linux的用户与组的概念一并理解,不了解的同学请参考Linux的用户和组 简介 在Linux中,每个文件除了有用户和组的信息以外,还有其对应的权限.可使用来查看. [r ...