CS-Script.Core C#通过读取文本实现动态脚本

安装包 CS-Script.Core

 编写脚本  工艺文件1.txt

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApp2;
 
   //注意不能有命名空间
    public class 工艺文件1 : IStep
    {
        public 工艺文件1()
        {
        }

        public void AddStep(List<StepInfo> stepInfos)
        {
            stepInfos.Add(new StepInfo { Name = "测试1", Time = 1234 });
            stepInfos.Add(new StepInfo { Name = "测试2", Time = 345 });
            stepInfos.Add(new StepInfo { Name = "测试3", Time = 3452 });
            stepInfos.Add(new StepInfo { Name = "测试4", Time = 3453 });
        }
    }
 
复制代码

文件放置位置

 代码1

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    public interface IStep

    {
        void AddStep(List<StepInfo> stepInfos);
    }

    public class StepInfo
    {
        public string Name { get; set; }
        public int Time { get; set; }
    }
}
复制代码

代码2

复制代码
using  Lib;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Threading;

namespace ConsoleApp2
{
    public class Program
    {
        private static void Main(string[] args)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "工艺文件1.txt";
            string res = File.ReadAllText(path);
            IStep script = CSScript.Evaluator

                                 .LoadCode<IStep>(res);
            List<StepInfo> stepInfos = new List<StepInfo>();
            script.AddStep(stepInfos);

              foreach (StepInfo stepInfo in stepInfos)
              {
                Console.WriteLine(stepInfo.Name + " " + stepInfo.Time);
              }

        }
    }
}
复制代码

 测试

 测试了一下比ironpython 快

github完整使用教程:

oleg-shilo/cs-script: C# scripting platform (github.com)

cs-script.core/src/CSScriptLib/src/CSScriptLib/samples.cs at master · oleg-shilo/cs-script.core (github.com)

posted @   JohnnyLei  阅读(179)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2021-12-12 C# 多线程等待子线程结束 Task.Factory
2021-12-12 C# 多线程等待子线程全部完成 ThreadPool
点击右上角即可分享
微信分享提示