出售域名 11365.com.cn
有需要请联系 16826375@qq.com
在手机上浏览
在手机上浏览

.net执行js代码

发布日期:2022-08-31

.net执行js代码?你没听错,确实有这么个好玩的东西

第一步通过Nuget引用 Jint

 

第二步就可以写代码开工了

using System;
using Jint;

namespace ConsoleApp1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine(PaddingLeft("abc"));
        }

        private static string PaddingLeft(string input)
        {
            var js = new Engine();
            js.SetValue("aa", input); //给变量赋值
            js.Execute("var bb=aa.padStart(5,'0');"); //执行完全是js的操作
            return js.GetValue("bb").ToString(); //读取变量的值
        }
    }
}

 

很神奇吧,还有更多的使用方法,参考开源地址:https://github.com/sebastienros/jint