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"));
            }
        }
    }
    

      

  • 相关阅读:
    私有云是伪命题:真正的私有云 ≈ 公有云
    云计算的重新构建架构:优化迁移策略
    五个顶级的大数据架构
    Algorithm Gossip: 费式数列
    Algorithm Gossip: 河内塔
    Mysql连接报错:Unknown system variable 'language'
    ssm整合的时候出现的abstactMethodArror 解决
    java集合整理
    Oracle的序列和索引
    关于java堆栈的理解与说明
  • 原文地址:https://www.cnblogs.com/oyjj/p/2134269.html
Copyright © 2011-2022 走看看