C#执行js

看到一个比较小巧的jint库。dll才244Kb的样子。在c#里面执行js脚本还蛮方便的。

1。执行js里的函数。

using Jint;
using System;

namespace RunJs
{
    class Program
    {
        static void Main(string[] args)
        {
            Engine e = new Engine();
            e.Execute("function add(a, b) { return a + b; }");
            var s = e.Invoke("add",1,2).ToObject();

            Console.WriteLine("{0}",s);
            Console.ReadKey();
            
        }
    }
}

2.取得js里的对象值。

using Jint;
using System;

namespace RunJs
{
    class Program
    {
        static void Main(string[] args)
        {
            Engine e = new Engine();
            e.Execute("function add(a, b) { return a + b};var j=add(3,6);");
            object s = e.GetValue("j").ToObject();
            
  
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#执行JavaScript代码,可以使用Google的V8引擎。V8是一个非常快速和高性能的JavaScript引擎,被广泛用于浏览器和Node.js等平台。 以下是一个简单的示例代码: ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Reflection; using System.Runtime.InteropServices; using System.Threading; using System.Diagnostics; using System.Security; namespace V8Demo { class Program { static void Main(string[] args) { // 加载V8引擎 var currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var v8Dir = Path.Combine(currentDir, "v8"); var v8DllPath = Path.Combine(v8Dir, "v8.dll"); NativeMethods.LoadLibrary(v8DllPath); // 初始化V8引擎 NativeMethods.V8_Init(); // 创建一个V8上下文 var context = NativeMethods.V8_CreateContext(); // 执行JavaScript代码 var result = NativeMethods.V8_ExecuteScript(context, "1 + 2"); // 输出结果 Console.WriteLine("Result: " + result); // 释放V8上下文 NativeMethods.V8_ReleaseContext(context); // 关闭V8引擎 NativeMethods.V8_Shutdown(); } } public static class NativeMethods { private const string V8DllName = "v8"; [DllImport(V8DllName, CallingConvention = CallingConvention.Cdecl)] public static extern bool LoadLibrary([MarshalAs(UnmanagedType.LPStr)] string lpFileName); [DllImport(V8DllName, CallingConvention = CallingConvention.Cdecl)] [SuppressUnmanagedCodeSecurity] public static extern void V8_Init(); [DllImport(V8DllName, CallingConvention = CallingConvention.Cdecl)] [SuppressUnmanagedCodeSecurity] public static extern IntPtr V8_CreateContext(); [DllImport(V8DllName, CallingConvention = CallingConvention.Cdecl)] [SuppressUnmanagedCodeSecurity] public static extern void V8_ReleaseContext(IntPtr context); [DllImport(V8DllName, CallingConvention = CallingConvention.Cdecl)] [SuppressUnmanagedCodeSecurity] public static extern IntPtr V8_ExecuteScript(IntPtr context, [MarshalAs(UnmanagedType.LPStr)] string script); [DllImport(V8DllName, CallingConvention = CallingConvention.Cdecl)] [SuppressUnmanagedCodeSecurity] public static extern void V8_Shutdown(); } } ``` 这个示例中,我们首先加载V8引擎的DLL文件,并通过NativeMethods类中的V8_Init()方法初始化V8引擎。然后,我们创建了一个V8上下文,并通过NativeMethods类中的V8_ExecuteScript()方法执行了一段简单的JavaScript代码。最后,我们释放了V8上下文,并通过NativeMethods类中的V8_Shutdown()方法关闭了V8引擎。 需要注意的是,这个示例仅仅是一个演示,实际使用中还需要考虑到内存管理、异常处理等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值