欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#执行Javascript代码的几种方法总结

程序员文章站 2024-02-25 18:08:39
一、开源项目 javascript .net 地址: 它是google chrome v8引擎在.net上的封装,功能完善,性能相对较高,推荐使用! (缺点就是asp.n...

一、开源项目 javascript .net

地址: 它是google chrome v8引擎在.net上的封装,功能完善,性能相对较高,推荐使用! (缺点就是asp.net web应用上,好象会报错)

示例代码:

复制代码 代码如下:

using noesis.javascript;
using system;
using system.collections.generic;

namespace jscsharp
{
    class program
    {
        static void main(string[] args)
        {
            using (javascriptcontext ctx = new javascriptcontext())
            {
                var i = ctx.run("1+2");
                console.writeline(i);//3
                ctx.run("var d = new date(2013,9-1,2,20,30,15)");
                var d = ctx.getparameter("d");
                console.writeline(d);//2013/9/2 20:30:15
                var j = ctx.run("function add(x,y){return x+y;};add(5,10);");
                console.writeline(j);//15
                ctx.run("var obj = {};obj.name='jimmy';obj.sex='male';obj.name='杨俊明'");
                var obj = ctx.getparameter("obj") as dictionary<string, object>;
                foreach (var key in obj.keys)
                {
                    console.writeline(string.format("{0}:{1}", key, obj[key]));
                    //name:杨俊明
                    //sex:male
                }
                var jsonarr = ctx.run("[{airport:'pek',name:'北京首都机场'},{airport:'xiy',name:'西安咸阳机场'}]") as array;
                foreach (var item in jsonarr)
                {
                    var json = item as dictionary<string, object>;
                    foreach (var key in json.keys)
                    {
                        console.writeline(string.format("{0}:{1}", key, json[key]));
                        //airport:pek
                        //name:北京首都机场
                        //airport:xiy
                        //name:西安咸阳机场
                    }
                }
                ctx.setparameter("x", 7);
                ctx.run("function add(x,y){return x+y;}; function sub(x,y){return x-y;};k = add(2,x);var t = sub(9,1);");
                var k = ctx.getparameter("k");
                console.writeline(k);//9
                var t = ctx.getparameter("t");
                console.writeline(t);//8
                console.read();
            }

        }

    }
}

部署时要注意,msvcp100.dll、msvcr100.dll这二个文件,要复制到bin目录下

二、利用microsoft.jscript.vsa

先添加microsoft.jscript、microsoft.vsa二个程序集的引用,然后就能用了,示例代码如下:

复制代码 代码如下:

using microsoft.jscript;
using microsoft.jscript.vsa;
using system;

namespace jscsharp
{
    class program
    {
        static vsaengine engine = vsaengine.createengine();

        static void main(string[] args)
        {
            var i = (int)evaljscript("1+2;");
            console.writeline(i);//3
            var d = (dateobject)evaljscript("var d = new date(2013,9-1,2,20,30,15)");
            console.writeline(d);//mon sep 2 20:30:15 utc+8 2013
            var j = evaljscript("function add(x,y){return x+y;};add(5,10);");
            console.writeline(j);//15
            var obj = evaljscript("var obj = {};obj.name='jimmy';obj.sex='male';");
            console.writeline(obj);//male 注:只返回最后一条执行的语句结果
            console.read();
        }

 
        static object evaljscript(string jscript)
        {
            object result = null;
            try
            {
                result = microsoft.jscript.eval.jscriptevaluate(jscript, engine);
            }
            catch (exception ex)
            {
                return ex.message;
            }
            return result;

        }
    }
}

vsa的功能比较有限,多条语句执行时,只返回最后一条语句的执行结果。