使用需求

有时候一段Javascript代码写的很棒,而我们又无法将之翻译成.net或翻译之成本很高的时候

我们就可以使用Jint引擎来运行Javascript代码,来得到我们想要的结果

或者上

http://jint.codeplex.com/releases/view/119215

下面代码使用的 0.9.2.0 版本

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Jint; namespace JIntTest
{
/// <summary>
/// 这里一这要是 public,否则JInt无法反射
/// </summary>
public class Student
{
public string Id { get; set; }
public string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{ //简单判断
var b = JavascriptUtil.Execute<int>("1>0", null);
//直接给参数赋值
       string str = "var stu={};stu.Id=Id;stu.Name=Name; return stu.Id";

            Dictionary<string, object> dict = new Dictionary<string, object>();
            dict.Add("Id", "");
dict.Add("Name", "Aven");
var c = JavascriptUtil.Execute<string>(str, dict); //通过类实例来赋值
Dictionary<string, object> dict2 = new Dictionary<string, object>();
var st = new Student//这里一定要使用public
{
Id = "",
Name = "Grace"
};
dict2.Add("ST",st);
var str2 = "var stu={};stu.Id=ST.Id;stu.Name=ST.Name; return stu.Id";
var d = JavascriptUtil.Execute<string>(str2, dict2); } public static string ReadFile(string path)
{
StreamReader sr = new StreamReader(path, Encoding.Default);
String line;
StringBuilder sb = new StringBuilder();
while ((line = sr.ReadLine()) != null)
{
sb.AppendLine(line); }
return sb.ToString(); } }
public static class JavascriptUtil
{ static JavascriptUtil()
{ } public static object Execute<T>(string expression, Dictionary<string, object> args)
{ if (string.IsNullOrEmpty(expression))
{
if (typeof(T) == typeof(int) || typeof(T) == typeof(decimal) || typeof(T) == typeof(double) || typeof(T) == typeof(float)) {
return ;
} if (typeof(T) == typeof(DateTime))
{
return DateTime.MinValue;
} if (typeof(T) == typeof(string))
{
return "";
} return default(T);
} if(typeof(T)==typeof(int))
{
var result = JintExecute(expression, args);
return Convert.ToInt32(result);
}
if (typeof(T) == typeof(decimal )) {
var result = JintExecute(expression, args);
return Convert.ToDecimal(result);
}
if (typeof(T) == typeof(double )) {
var result = JintExecute(expression, args);
return Convert.ToDouble(result);
}
if (typeof(T) == typeof(DateTime )) {
var result = JintExecute(expression, args);
return Convert.ToDateTime(result);
} return JintExecute(expression, args);
} public static object JintExecute(string expression, Dictionary<string, object> args)
{
var engine = _engine; if (args != null)
{
foreach (var x in args)
{
engine.SetParameter(x.Key, x.Value);
}
}
return engine.Run(expression) ;
} private static JintEngine _engine= new JintEngine(); }
}

Jint .net平台的javascript引擎的更多相关文章

  1. 浏览器-09 javascript引擎和Chromium网络栈

    语言的运行 C/C++语言 使用编译器直接将它们编译成本地代码(机器指令),这是由开发人员在代码编写完成之后实施; 用户只是使用这些编译好的本地代码,这些本地代码被系统的加载器加载执行,由操作系统调度 ...

  2. 微软Edge 内嵌的JavaScript 引擎即将开源

    微软于今日(2015年12月10日)宣布即将开源Chakra核心控件,并改名为“ChakraCore”,该控件包含所有Edge JavaScript 引擎的所有核心功能.ChakraCore 将于下月 ...

  3. 理解WebKit和Chromium: JavaScript引擎简介

    转载请注明原文地址:http://blog.csdn.net/milado_nju 1. 什么是JavaScript引擎 什么是JavaScript引擎?简单来讲,就是能够提供执行JavaScript ...

  4. 【转】理解WebKit和Chromium: JavaScript引擎简介

    转载请注明原文地址:http://blog.csdn.net/milado_nju1. 什么是JavaScript引擎什么是JavaScript引擎?简单来讲,就是能够提供执行JavaScript代码 ...

  5. V8 javascript 引擎

    V8是一个由丹麦Google开发的开源java script引擎,用于Google Chrome中.[2]Lars Bak是这个项目的组长.[3]   V8在执行之前将java script编译成了机 ...

  6. javascript与java的相互调用,纯java的javascript引擎rhino(转载)

    1.下载Rhino安装包,下载地址:官网http://www.mozilla.org/rhino. 2.rhino环境配置,把解压出来的js.jar文件加入到系统的环境变量classpath 3.在命 ...

  7. 关于浏览器内核与javascript引擎的一些小知识

    浏览器是我们每天几乎都必须使用的软件产品,可是对于自己每天都接触的浏览器,很多同学其实对其一无所知.今天异次元就跟大家说说关于浏览器内核的一些事儿吧,好让你了解多一点稍微内在的东西. 在下面的文章中主 ...

  8. JavaScript 引擎 V8 执行流程概述

    本文首发于 vivo互联网技术 微信公众号 链接:https://mp.weixin.qq.com/s/t__Jqzg1rbTlsCHXKMwh6A作者:赖勇高 本文主要讲解的是V8的技术,是V8的入 ...

  9. [WebKit内核] JavaScript引擎深度解析--基础篇(一)字节码生成及语法树的构建详情分析

    [WebKit内核] JavaScript引擎深度解析--基础篇(一)字节码生成及语法树的构建详情分析 标签: webkit内核JavaScriptCore 2015-03-26 23:26 2285 ...

随机推荐

  1. spring mvc处理静态资源

    servlet的url映射定义为'/'表示映射全部路径 struts的过滤器是*.action,在spring mvc中设置成*.action或者*.do......也是可以的,但是spring mv ...

  2. 由 excel 转换为 markdown,及收获

    由 excel 转换为 markdown,及收获 1 问题 构建之法(现代软件工程)东北师大站[http://www.cnblogs.com/younggift/]的每周学生作业成绩,执行教学团队[h ...

  3. Swift开发小技巧--TabBar中间按钮的添加方案

    TabBar中间按钮的添加方案 之前做百思项目的时候,也有一个中间按钮,当时是重写的TabBar,这里介绍一个新的方法 给TabbarVC多添加添加一个控制器,这个控制器的作用仅仅是用来占位的,多了这 ...

  4. 【POJ 2774】Long Long Message 最长公共子串

    还是模板啊,手残&&打成||查错查了1h+TAT #include<cstdio> #include<cstring> #include<algorith ...

  5. python 进程间共享数据 (一)

    def worker(num, mystr, arr): num.value *= 2 mystr.value = "ok" for i in range(len(arr)): a ...

  6. C#-WinForm-无边框窗体的移动和阴影-API

    //窗体移动API,先导入命名空间,在委托MouseDown事件 //移动前准备 [DllImport("user32.dll")] public static extern bo ...

  7. smtplib.SMTPDataError: (554, 'DT:SPM 126 smtp5错误解决办法

    1.自动化测试中,调用邮件模块自动发送邮件时,运行脚本报错: smtplib.SMTPDataError: (554, 'DT:SPM 126 smtp5,jtKowAD3MJz2c1JXLcK2AA ...

  8. SSH无密码登陆Agent admitted failure to sign using the key

    A :CentOS_Master B:Slave_1 C:Slave_2 普通用户hxsyl 1.现在A 上 ssh-keygen -t rsa 一路回车,不需要输入密码 执行该操作将在/home/h ...

  9. cmd执行命令返回执行信息

    /// <summary> /// 执行CMD语句 /// </summary> /// <param name="cmd">要执行的CMD命令 ...

  10. android下载简单工具类

    功能是实现下载文件,图片或MP3等,为了简单起见使用单线程,此代码为MarsAndroid教程的复制品,放在此处,留着参考. 首先是一个得到字节流随后保存到内存卡上的工具类: package com. ...