前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >利用IronJs在.NET程序里面跑javascript脚本

利用IronJs在.NET程序里面跑javascript脚本

作者头像
MJ.Zhou
发布2018-01-04 16:23:36
9680
发布2018-01-04 16:23:36
举报
文章被收录于专栏:.NET开发那点事.NET开发那点事

what’s dlr

The dynamic language runtime (DLR) is a runtime environment that adds a set of services for dynamic languages to the common language runtime (CLR). The DLR makes it easier to develop dynamic languages to run on the .NET Framework and to add dynamic features to statically typed languages.

Dynamic languages can identify the type of an object at run time, whereas in statically typed languages such as C# and Visual Basic (when you use Option Explicit On) you must specify object types at design time. Examples of dynamic languages are Lisp, Smalltalk, JavaScript, PHP, Ruby, Python, ColdFusion, Lua, Cobra, and Groovy.

这么一堆洋文摆着,也懒的翻译了。说直接一点就是DLR使得.NET有了可以执行脚本语言的能力(也许描述的不太精准,不过你可以这么理解)。

why use IronJs

基于DLR微软自己开了两套类库来跑python跟ruby。不过我想javascript的通用性更强,做程序员的,不过100%也得有90%写过javascript吧。

what problem can be solved

那么这种能力有什么好处呢。我能想到的就是对于系统中一些经常需要变更的逻辑,比如折扣算法,积分,以及各种规则,我们可以提到脚本里去写。这样不用任何编译,ctrl+s一下就可以解决问题了。

this is demo:

code:

代码语言:javascript
复制
            var jsContext = new IronJS.Hosting.CSharp.Context();
            jsContext.ExecuteFile("myDlr.js");
            var fun = jsContext.GetFunctionAs<Func<double, double, double>>("cacl");
            double a = Double.Parse(this.tbxA.Text);
            double b = Double.Parse(this.tbxB.Text);
            var result = fun.Invoke(a,b);
            this.tbxResult.Text = result.ToString();

js:

代码语言:txt
复制

            var cacl = function (a, b) {
            return a*b;
           };
image
image

当我修改a*b为a-b的时候结果直接就变成-10了。不用关闭程序,不用编译程序,爽。

image
image
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-11-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • what’s dlr
  • why use IronJs
  • what problem can be solved
  • this is demo:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档