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完整使用教程: