c#,解释和执行c#代码;

控制LED真是一件很麻烦的事情,因为底层协议一个个字节进行了定义;一个字节定义错误,就会得到错误结果;

更麻烦的是,流明电子公司定义的底层协议啊,一会高位在前,一会低位在前,真不明白到底是怎么想的;

所以,才有这样一个想法,在一个文本框中写代码,来动态执行;

看到这篇帖子,http://www.cnblogs.com/zlgcool/archive/2008/10/12/1309616.html

拷贝,粘贴,就出来如下的代码了。。

唯一多了两行,引用了自身的程序集和添加了using; 

因为我必须能够调用程序中包含的根据dll转换而来的C# API函数;

       void Execute(String code)
        {
            try
            {
                var srcText = BuildCode(code, this.withResult.Checked);
                // 1.Create a new CSharpCodePrivoder instance
                CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
                // 2.Sets the runtime compiling parameters by crating a new CompilerParameters instance
                CompilerParameters objCompilerParameters = new CompilerParameters();
                objCompilerParameters.ReferencedAssemblies.Add("System.dll");
                objCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
                objCompilerParameters.ReferencedAssemblies.Add("DynamicCSharpWin.exe");
                objCompilerParameters.GenerateInMemory = true;
                // 3.CompilerResults: Complile the code snippet by calling a method from the provider
                CompilerResults cr = objCSharpCodePrivoder.CompileAssemblyFromSource(objCompilerParameters, srcText);
                if (cr.Errors.HasErrors)
                {
                    string strErrorMsg = cr.Errors.Count.ToString() + " Errors:";
                    for (int x = 0; x < cr.Errors.Count; x++)
                    {
                        strErrorMsg = strErrorMsg + "\r\nLine: " +
                                     cr.Errors[x].Line.ToString() + " - " +
                                     cr.Errors[x].ErrorText;
                    }
                    log(strErrorMsg);
                    MessageBox.Show("There were build erros, please modify your code.", "Compiling Error");
                    return;
                }
                // 4. Invoke the method by using Reflection
                Assembly objAssembly = cr.CompiledAssembly;
                var compileType = objAssembly.GetType("C");
                if (compileType == null)
                {
                    log("Error: " + "Couldn't load class.");
                    return;
                }
                object[] objCodeParms = new object[1];
                MethodInfo methodInfo = compileType.GetMethod("T");
                object result = methodInfo.Invoke(null, null);
                if (withResult.Checked)
                {
                    log(result.ToString());
                }
            }
            catch (Exception ee)
            {
                log(ee.Message);
                log(ee.StackTrace);                
            }
        }
        String BuildCode(String code, bool withResult)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("using System;");
            builder.Append("using DynamicCSharpWin;");
            builder.Append("public class C{ ");
            if (withResult) { builder.Append("public static object T(){"); }
            else { builder.Append("public static void T(){"); }
            builder.Append(code);
            builder.Append("}}");
            return builder.ToString();
        }
 



paxscriptnet2.7(价值187美金)C#代码解释执行器 About paxScript.NET 18 December 2013. Support of MS Visual Studio 2013. The Microsoft NET version of the paxScript scripting engine (paxScript.NET) includes interpreters of C# and VB.NET languages. The key features of paxScript.NET are: paxScript.NET is written in C#. Source code of paxScript.NET is CLS compilant. Support of Silverlight and Windows Phone platforms of Microsoft .NET Framework. Support of Mono. paxScript.NET compiles programs into byte-code. It does not use CodeDOM and it does not generate a dynamic assembly. Separate compilation of modules is allowed. You can combine source code modules and compiled (binary) modules in your paxScript.NET script project. Cross-language scripting is allowed. You can combine modules written in C# and VB.NET in your paxScript.NET script project. C# interpreter is based on the ECMA-334 standard. It supports all C# language features with exception for the unsafe code, attributes and generic types. The interpreter extends standard of C# language with extra features which simplify the use of C# for scripting needs (more...). paxScript.NET is implemented as a .NET component ( paxscript-net.dll) which can be used with Microsoft Visual Studio .NET and Mono. The component allows you to embed paxScript.NET interpreter into your WinForms, Mobile or ASP.NET application so you can customize and extend the application without having to recompile it. Any classes, structures, enumerations, arrays, delegates, events and interfaces of host application can be used in a script. (See demo...). You call script-defined methods from host application. (See demo...). Debug capabilities: breakpoints, call stack, watch/evaluate, trace into, step over etc. (See demo...).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值