编写高度用户可扩展的C#应用程序的最佳实践
我目前在一家研究机构,负责微机械/精密机械,并被要求为我们在我们的一个实验室中使用的当前设置开发控制器软件。
我们有一个纳米级和一些其他设备,如百叶窗和滤光轮,应该通过中央应用程序进行控制。 该软件提供了用于执行的预配置作业,这些算法基本上是为阶段生成命令的算法,用于将激光写入样本的不同模式(例如矩形,圆柱等)
现在,提供某种在运行时扩展此预定义作业列表的可能性会很棒,这意味着用户可以添加所提供的算法。
我是一个C#新手(以及一般的桌面应用程序新手),所以我非常感谢你能给我一些关于如何做到这一点或者我应该从哪里开始寻找的提示。
我使用.NET集成的C#编译器做了这个’脚本’的事情。
这是一些工作,但基本上是这样的:
public Assembly Compile(string[] source, string[] references) { CodeDomProvider provider = new CSharpCodeProvider(); CompilerParameters cp = new CompilerParameters(references); cp.GenerateExecutable = false; cp.GenerateInMemory = true; cp.TreatWarningsAsErrors = false; try { CompilerResults res = provider.CompileAssemblyFromSource(cp, source); // ... return res.Errors.Count == 0 ? res.CompiledAssembly : null; } catch (Exception ex) { // ... return null; } } public object Execute(Assembly a, string className, string methodName) { Type t = a.GetType(className); if (t == null) throw new Exception("Type not found!"); MethodInfo method = t.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); // Get method if (method == null) throw new Exception("Method '" + methodName + "' not found!"); // Method not found object instance = Activator.CreateInstance(t, this); object ret = method.Invoke(instance, null); return ret; }
真正的代码可以做很多事情,包括代码编辑器。
它在我们的工厂多年来一直很好用。
这样,用户可以使用C#作为脚本,因此可以使用与您相同的API。
您可以使用看起来像普通.cs
文件的代码模板。 它是在运行时构建的,并提供给Compile
的source
参数。
using System; using System.IO; using ... namespace MyCompany.Stuff { public class ScriptClass { public object main() { // copy user code here // call your own methods here } // or copy user code here private int test(int x) { /* ... */ } } }
例:
上述就是C#学习教程:编写高度用户可扩展的C#应用程序的最佳实践分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
string[] source = ??? // some code from TextBoxes, files or whatever, build with template file... string[] references = new string[] { "A.dll", "B.dll" }; Assembly a = Compile(source, references); object result = Execute(a, "MyCompany.Stuff.ScriptClass", "main");
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1033586.html