C#脚本编辑器和引擎

SuperScript是C#脚本引擎,它为您的WinForms和WPF应用程序带来了高效的代码编辑功能。

它提供了代码编辑功能,如语法高亮显示、智能代码提示和代码完成、语法错误,

它支持类库引用,编译及导出等等,使用它好比您在Visual Studio中编码一样,速度和便利性相媲美。

使用它,可以极大的发挥您的应用程序扩展和开放性,

使得您的程序在发布后,也可支持用户自定义编辑和运行脚本。

下载地址:https://gitee.com/dev2022/super-script-community.git

以下是扩展简单示例:

比如您的应用程序有个计算功能,它允许用户根据用户输入值,通过编辑脚本自定义输出结果功能,如实现字符串倒叙功能。

脚本调试:

程序发布后,如何进行脚本代码调试,目前脚本编辑器调试版本还在研发测试中。不过,可以通过下面这种方式进行调试!

只要电脑上安装visual studio即可(vs2010及以上版本等都可以),这样就可以使用vs的强大调试功能了。

如何使用Visual Studio进行调试:

(1)启用调试模式

(2)打开VS附加程序进程

(3)运行程序,即可命中断点跳转到vs并指向代码断点位置

示例代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using BCL.SuperScript;
using BCL.SuperScript.Engine;
using System.Diagnostics;

namespace TestScript
{
    public partial class FrmTest : Form
    {
        /// <summary>脚本对应的基类</summary>
        private CalcBase calcBase = null;

        //每个脚本文件都要对应一个基类,下面定义一个计算的基类
        public class CalcBase
        {
            //这里定义了一个计算的方法,当然这里还可以定义其他更多方法

            /// <summary>计算</summary>
            /// <param name="input">输入</param>
            /// <returns>输出</returns>
            public virtual string Calc(string input)
            {
                string output = input;
                return output;
            }
        }


        /// <summary>脚本构造器</summary>
        private WagScriptBuild<CalcBase> build = null;

        //脚本可以存储到文件,数据库表中,等等,下面以脚本存储到文件为示例
        //程序目录下放了2个脚本文件:calc_template.script 和 calc_custom.script
        //脚本文件的即是基类的默认实现子类,格式和内容可查看2个示例文件

        /// <summary>默认初始化脚本读取和存储的文件路径</summary>

        private string _file_script_template = Path.Combine(Application.StartupPath, "calc_template.script");

        /// <summary>自定义脚本读取和存储的文件路径</summary>
        private string _file_script_custom = Path.Combine(Application.StartupPath, "calc_custom.script");



        /// <summary>脚本内容</summary>
        private string _script = "";

        public FrmTest()
        {
            InitializeComponent();

            //软件启动的地方加上脚本组件的注册(社区版免费版授权)
            bool regSuccess = false;
            string regMessage = "";
            regSuccess = WagScriptLic.Reg("community", "117faef434b61d2f1e906ba261877025302fa9f9c8e6701ae2e5d6a32436df02dbfb99cab52e908bf3a52ec33d989b4862d116ee5b1ab5a2b6ba2cf2ad0eaf582f870d56966c3fce6452b781f6cfb37558eac161ce592eebf7362aea010c8cd8d879f1c1242ac1573dff9cad14f8fbdc98fb92f78002e6e3c009c9752b18e1b0fc1d1aafec259bf668402652aa2c6abb49264b278562175f24b95168da61d96b3d5ea460b9fb94dd396ddb1eb898dd6008f9efdad7ceb978ba82b884fe0d69cf42a0d3f2d10236cc521a94ef068e38533045f945fb601424b8aa729360f9dcb13b0b87d95be61b9a87f2b384a5080feeddb0faa28a30b4c78449769de83ddfa1490d783893d20a29f9a61d5e6b7494d62d90b271ec8f606db1d5de7b4035845c2f5bcbcec0d487449d97dafd14d0a7f8", ref regMessage);
            if (!regSuccess)
            {
                MessageBox.Show(regMessage, "Reg");
            }

            //读取文件中的脚本内容

            //如果用户首次使用脚本,则加载默认脚本模板,否则使用上次存储的脚本
            if (!File.Exists(_file_script_custom))
            {
                if (!File.Exists(_file_script_template))
                {
                    MessageBox.Show($"未找到脚本模板(模板脚本),请检查:{_file_script_template}");
                    this.btn_srcipt.Enabled = false;
                    return;
                }

                File.Copy(_file_script_template, _file_script_custom);
            }

            string data = File.ReadAllText(_file_script_custom, Encoding.UTF8);
            _script = data;

        }

        private void _Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(_script))
            {
                build = new WagScriptBuild<CalcBase>(_script);

                calcBase = build.BuildScript(false);//编译脚本

                if (calcBase == null)
                {
                    MessageBox.Show($"脚本编译异常: {build.m_sScriptError}");
                    calcBase = new CalcBase();
                }
            }
            else
            {
                calcBase = new CalcBase();
            }
        }


        /// <summary>修改脚本</summary>
        private void btn_srcipt_Click(object sender, EventArgs e)
        {
            build.EditExistingScript(true);

            string s = build.m_WagScriptSupport.SaveToStr("");
            _script = s;

            File.WriteAllText(_file_script_custom, s, Encoding.UTF8);//存储修改后的脚本到文件

            calcBase = build.BuildScript(false);//再次编译脚本

        }

        /// <summary>计算</summary>
        private void btn_run_Click(object sender, EventArgs e)
        {
            string input = this.txt_input.Text.Trim();
            string output = this.calcBase.Calc(input);
            this.txt_output.Text = output;
        }

    }
}

  • 2
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值