• 使用codedom 编写脚本解释器


    本篇博客的目的是为了保存例子,怕自己忘记。

     1 private void dd(string code)
     2         {
     3             string path = "BonkerSpace";
     4             if (File.Exists(path))
     5             {
     6                 File.Delete(path);
     7             }
     8             // 创建编译器对象
     9             Microsoft.CSharp.CSharpCodeProvider p = new Microsoft.CSharp.CSharpCodeProvider();
    10             //new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
    11 
    12             // 设置编译参数
    13             System.CodeDom.Compiler.CompilerParameters options = new System.CodeDom.Compiler.CompilerParameters();
    14             options.ReferencedAssemblies.Add("System.dll");
    15             options.ReferencedAssemblies.Add("System.Data.dll");
    16             options.ReferencedAssemblies.Add("System.Xml.dll");
    17             options.ReferencedAssemblies.Add("mscorlib.dll");
    18             options.GenerateInMemory = true;
    19             options.OutputAssembly = "BonkerSpace";
    20 
    21             // 开始编译
    22             System.CodeDom.CodeSnippetCompileUnit cu = new System.CodeDom.CodeSnippetCompileUnit(code);
    23             System.CodeDom.Compiler.CompilerResults cr = p.CompileAssemblyFromDom(options, cu);
    24 
    25             // 执行动态程序集相关内容。
    26             Type t = cr.CompiledAssembly.GetType("BonkerSpace.BonkerClass");
    27             object o = cr.CompiledAssembly.CreateInstance("BonkerSpace.BonkerClass");
    28             object result = t.InvokeMember("Bonker", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.InvokeMethod, null, o, null);
    29 
    30             this.richTextBox2.Text = result.ToString();
    31         }

    这个方法是codedome的解释类。code参数是代码,注意此代码是完整的c#代码

    例如你可以这样使用

     1 private void button1_Click(object sender, EventArgs e)
     2         {
     3             try
     4             {
     5                
     6                 string msg = string.Format(@"
     7                     using System;
     8                     using System.Collections.Generic;
     9                    // using System.ComponentModel;
    10                     using System.Data;
    11                    // using System.Drawing;
    12                    // using System.Linq;
    13                     //using System.Text;
    14                    // using System.Windows.Forms;
    15                     namespace BonkerSpace
    16                     {{
    17                          public class BonkerClass
    18                         {{
    19                             public object Bonker()
    20                             {{
    21                                 {0}
    22                             }}
    23                         }}
    24                     }}", this.richTextBox1.Text);
    25                 dd(msg);
    26             }
    27             catch (Exception ex)
    28             {
    29                 MessageBox.Show(ex.Message);
    30             }
    31 
    32         }

    这样就可以了。

    demo下载

  • 相关阅读:
    android常用工具类
    SharedPreferences的工具类
    Dialog对话框管理工具类
    Logger日志管理工具类
    android 复制、粘贴文字
    sd卡文件操作
    AndroidManifest.xml file missing 解决方案
    Jar mismatch! Fix your dependencies
    时间戳和字符串之间的互相转换
    常见块元素和内联元素
  • 原文地址:https://www.cnblogs.com/Bonker/p/3503365.html
Copyright © 2020-2023  润新知