zoukankan      html  css  js  c++  java
  • C#代码执行者1.0

    动态生成代码程序收集NO1:

    using System;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Text;
    using System.CodeDom;
    using System.CodeDom.Compiler;

     

    namespace csrepl {
        
    class Program {

     

            
    static string funcprefix = "using System; "
                
    + "public delegate void Proc(); "
                
    + "public class Wrapper {  "
                
    + "  public static object Set(string name, object value) {  "
                
    + "    AppDomain.CurrentDomain.SetData(name, value); "
                
    + "    return value;  "
                
    + "  } "
                
    + "  public static object Get(string name) {  "
                
    + "    return AppDomain.CurrentDomain.GetData(name); "
                
    + "  } "
                
    + "  public static object Invoke(Proc proc) {  "
                
    + "    proc(); "
                
    + "    return null;  "
                
    + "  } "
                
    + "  public static object Eval() { return  ";
            
    static string funcsuffix = ";  } }";

     


            
    static string StringEval(string expr) {
                
    string program = funcprefix + expr + funcsuffix;

     

                ICodeCompiler compiler 
    = CodeDomProvider.CreateProvider("C#").CreateCompiler();
                CompilerParameters cp 
    = new CompilerParameters();
                cp.GenerateExecutable 
    = false;
                cp.GenerateInMemory 
    = true;

     

                CompilerResults results 
    = compiler.CompileAssemblyFromSource(cp, program);
                
    if (results.Errors.HasErrors) {
                    
    if (results.Errors[0].ErrorNumber == "CS0029")
                        
    return StringEval("Invoke(delegate { " + expr + "; })");
                    
    return results.Errors[0].ErrorText;
                }
                
    else {
                    Assembly assm 
    = results.CompiledAssembly;
                    Type target 
    = assm.GetType("Wrapper");
                    MethodInfo method 
    = target.GetMethod("Eval");
                    
    object result = method.Invoke(nullnull);
                    
    return result == null ? null : result.ToString();
                }
            }

     

            
    static void Main(string[] args) {

     

                
    while (true ) {
                    Console.Write(
    "");
                    Console.Out.Flush();
                    
    string expr = Console.ReadLine();
                    
    if (expr == null)
                        
    break;
                    
    try {
                        
    string result = StringEval(expr);
                        Console.WriteLine(result);
                    }
                    
    catch (TargetInvocationException ex) {
                        Console.WriteLine(ex.InnerException.GetType().Name 
    + "" + ex.InnerException.Message);
                    }
                    
    catch (Exception ex) {
                        Console.WriteLine(ex.GetType().Name 
    + "" + ex.Message);
                    }
                }

            }
        }
    }


    代码2:

    private void Compiler(string code)
            {
                CompilerParameters vCompilerParameters 
    = new CompilerParameters();
                vCompilerParameters.GenerateExecutable 
    = false;
                vCompilerParameters.GenerateInMemory 
    = true;
                vCompilerParameters.ReferencedAssemblies.Add(
    "System.Windows.Forms.dll");
                
    string vSource =
                    
    "using System.Windows.Forms;\n" +
                    
    "public class Temp\n" +
                    
    "{\n" +
                    
    "    public void Test()\n" +
                    
    "    {\n" +
                    
    "         " + code + "\n" +
                    
    "    }\n" +
                    
    "}\n";
                CompilerResults vCompilerResults 
    =
                    CodeDomProvider.CreateProvider(
    "CSharp").CompileAssemblyFromSource(vCompilerParameters, vSource);

                Assembly vAssembly 
    = vCompilerResults.CompiledAssembly;
                
    object vTemp = vAssembly.CreateInstance("Temp");
                MethodInfo vTest 
    = vTemp.GetType().GetMethod("Test");
                vTest.Invoke(vTemp, 
    null);
            }

     

    string code = "int sum = 0;\nfor(int i = 0; i < 100; i++)\n{\nsum += i;\n}\nMessageBox.Show(sum.ToString());";
                Compiler(code);




    代码执行者1.0

    功能:

           在TextBox中输入一段C#代码,点击按钮即可运行该代码。

    界面预览:




    本程序源码参见上面动态编译的例子。

    演示程序下载 : Demo
  • 相关阅读:
    MySQL设计之三范式的理解
    git基本操作命令和安装
    MySQL 中<=>用法(长知识)
    举个栗子看如何做MySQL 内核深度优化
    我们来说一说TCP神奇的40ms
    一览js模块化:从CommonJS到ES6
    Vtiger CRM 几处SQL注入漏洞分析,测试工程师可借鉴
    做优化的数据库工程师请参考!CynosDB的计算层设计优化揭秘
    1个开发如何撑起一个过亿用户的小程序
    教你一个vue小技巧,一般人我不说的
  • 原文地址:https://www.cnblogs.com/tuyile006/p/866152.html
Copyright © 2011-2022 走看看