• C#实现动态编译代码


    /*------------------------------------------------------------------------------
     *  Copyright (C) 2013 
     *  版权所有。
     *  
     *  文件名:     Compile.cs
     *  功能說明:   
     *
     *  創建人:    it_Eric
     *  創建時間:   2013/10/25 16:19:52
     *   
     *  修改人:    
     *  修改說明:    
     * 
    *-----------------------------------------------------------------------------*/
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.CodeDom.Compiler;
    using Microsoft.CSharp;
    using System.IO;
    using System.Reflection;
    
    namespace TestDemo
    {
        public  class Compile
        {
            private string prefix = "using System;" +
                "public static class Driver" +
                "{" +
                   "public static void Run()" +
                   "{";
            private string postfix =
                    "}" +
                 "}";
            public string CompileAndRun(string input, out bool hasError)
            {
                hasError = false;
                string returnData = null;
    
                CompilerResults results = null;
                using (var provider = new CSharpCodeProvider())
                {
                    var options = new CompilerParameters();
                    options.GenerateInMemory = true;
                    var sb = new StringBuilder();
                    sb.Append(prefix);
                    sb.Append(input);
                    sb.Append(postfix);
                    results = provider.CompileAssemblyFromSource(options, sb.ToString());
                }
                if (results.Errors.HasErrors)
                {
                    hasError = true;
                    var errorMessage = new StringBuilder();
                    foreach (CompilerError error in results.Errors)
                    {
                        errorMessage.AppendFormat("{0}{1}", error.Line, error.ErrorText);
                        returnData = errorMessage.ToString();
                    }
    
                }
                else
                {
                    TextWriter temp = Console.Out;
                    var writer = new StringWriter();
                    Console.SetOut(writer);
                    Type drivertype = results.CompiledAssembly.GetType("Driver");
                    drivertype.InvokeMember("Run", System.Reflection.BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, null, null, null);
    
                    Console.SetOut(temp);
                    returnData = writer.ToString();
    
                }
                return returnData;
            }
                    
    
    
        }
    }
  • 相关阅读:
    大航海计划
    副业刚需
    【转】iOS学习之适配iOS10
    【原】iOS学习之Masonry第三方约束
    【原】iOS学习之PINCache第三方缓存框架
    【原】iOS学习之苹果原生代码实现Autolayout和VFL语言
    【原】iOS学习之NSDate在项目中的一些类目扩展
    【原】iOS学习之tableView的常见BUG
    【转】iOS开发 -- Apple Pay
    【原】iOS学习之事件处理的原理
  • 原文地址:https://www.cnblogs.com/anbylau2130/p/3388184.html
Copyright © 2020-2023  润新知