C# Javascript引擎,如何在C#中執行現有的Javacript代碼?

來源:互聯網
上載者:User

請下載這個Assembly。提供了.net3.5和.net4.0版本使用方法1.下載Noesis.Javascript.dll2.Add Referene 加到工程3.運行官方網站上給的Demo 4.建立Console工程
CSJS

後附完整代碼:

運行後可獲得命令列輸出。http://javascriptdotnet.codeplex.com/
這個是對Google's V8 Javascript engine的封裝,最好的Javascript引擎了。有了這個我們就可以將一些現有的Javascirpt代碼引入到C#的代碼中運行了。

Project Description

Javascript .NET integrates Google's V8 Javascript engine and exposes it to the CLI environment. Javascript .NET compiles (at runtime) and executes scripts directly from .NET code. It allows CLI objects to be exposed and manipulated directly from the executed
Javascript.Sample "Hello World" running through Javascript

using System;using System.Collections.Generic;using System.Linq;using System.Text;using Noesis.Javascript;namespace CSJS{    class Program    {        public class SystemConsole        {            public SystemConsole() { }            public void Print(string iString)            {                Console.WriteLine(iString);            }        }        static void Main(string[] args)        {            // Initialize a context            JavascriptContext context = new JavascriptContext();            // Setting external parameters for the context            context.SetParameter("console", new SystemConsole());            context.SetParameter("message", "Hello World !");            context.SetParameter("number", 1);            // Script            string script = @"    var i;    for (i = 0; i < 5; i++)        console.Print(message + ' (' + i + ')');    number += i;";            // Running the script            context.Run(script);            // Getting a parameter            Console.WriteLine("number: " + context.GetParameter("number"));        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.