运IronJs在.NET程序中跑javascript脚本

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:

            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:

 

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

图片 1

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

图片 2

相关文章