zoukankan      html  css  js  c++  java
  • C# Javascript引擎,如何在C#中执行现有的Javacript代码?

    请下载这个Assembly。提供了.net3.5和.net4.0版本

    http://javascriptdotnet.codeplex.com/

    使用方法
    1.下载Noesis.Javascript.dll
    2.Add Referene 加到工程
    3.运行官方网站上给的Demo 
    4.我新建工程CSJS
    后附完整代码,运行后可获得命令行输出。
    这个是对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"));
            }
        }
    }
    

      

  • 相关阅读:
    bzoj3653: 谈笑风生
    bzoj1858: [Scoi2010]序列操作
    bzoj1857: [Scoi2010]传送带
    bzoj1856: [Scoi2010]字符串
    bzoj1855: [Scoi2010]股票交易
    bzoj1854: [Scoi2010]游戏
    bzoj1853: [Scoi2010]幸运数字
    斜堆,非旋转treap,替罪羊树
    NOI2003 文本编辑器
    A_star poj2449 k短路
  • 原文地址:https://www.cnblogs.com/oyjj/p/2134269.html
Copyright © 2011-2022 走看看