C#動態編譯

來源:互聯網
上載者:User

標籤:com   屬性   oba   動態   ras   ram   env   包含   odi   

原文:http://www.cnblogs.com/jailu/archive/2007/07/22/827058.html

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Reflection;using System.Globalization;using Microsoft.CSharp;using System.CodeDom;using System.CodeDom.Compiler;namespace ConsoleApplication1{    /// <summary>    /// C#動態編譯    /// </summary>    class Program    {        static void Main(string[] args)        {            // 1.CSharpCodePrivoder            //提供對C#代碼產生器和代碼編譯器的執行個體的訪問。如果要動態產生VB代碼,可以使用VBCodeProvider。            //CreateCompiler():擷取編譯器的執行個體。            CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();            // 2.ICodeComplier            //定義用於調用原始碼編譯的介面或使用指定編譯器的CodeDOM樹。            //每種編譯方法都接受指示編譯器的CompilerParameters對象,並返回指示編譯結果的CompilerResults對象。            //CompilerAssemblyFromSource(CompilerParameters option, string source):            //使用指定的編譯器,從包含原始碼的字串設定編譯器集。            ICodeCompiler objICodeCompiler = objCSharpCodePrivoder.CreateCompiler();                        // 3.CompilerParameters            //表示用於調用編譯器的參數。            //ReferencedAssemblies:擷取當前項目所引用的程式集。Add方法為程式集添加引用。            //GenerateExecutable:擷取或設定一個值,該值指示是否產生可執行檔。若此屬性為false,則產生DLL,預設是false。            //GenerateInMemory:擷取或設定一個值,該值指示是否在記憶體中產生輸出。            CompilerParameters objCompilerParameters = new CompilerParameters();            //objCompilerParameters.ReferencedAssemblies.Add("System.dll");            objCompilerParameters.ReferencedAssemblies.Add("System.dll");            objCompilerParameters.GenerateExecutable = false;            objCompilerParameters.GenerateInMemory = true;            // 4.CompilerResults            //表示從編譯器返回的編譯結果。            //CompiledAssembly:擷取或設定以編譯的程式集,Assembly類型。            CompilerResults cr = objICodeCompiler.CompileAssemblyFromSource(objCompilerParameters, GenerateCode());            //5.產生的程式集            if (cr.Errors.HasErrors)            {                Console.WriteLine("編譯錯誤:");                foreach (CompilerError err in cr.Errors)                {                    Console.WriteLine(err.ErrorText);                }            }            else            {                // 通過反射,調用HelloWorld的執行個體                Assembly objAssembly = cr.CompiledAssembly;                object objHelloWorld = objAssembly.CreateInstance("DynamicCodeGenerate.HelloWorld");                MethodInfo objMI = objHelloWorld.GetType().GetMethod("OutPut");                Console.WriteLine(objMI.Invoke(objHelloWorld, null));            }            Console.ReadLine();        }        static string GenerateCode()        {            StringBuilder sb = new StringBuilder();            sb.Append("using System;");            sb.Append(Environment.NewLine);            sb.Append("namespace DynamicCodeGenerate");            sb.Append(Environment.NewLine);            sb.Append("{");            sb.Append(Environment.NewLine);            sb.Append("    public class HelloWorld");            sb.Append(Environment.NewLine);            sb.Append("    {");            sb.Append(Environment.NewLine);            sb.Append("        public string OutPut()");            sb.Append(Environment.NewLine);            sb.Append("        {");            sb.Append(Environment.NewLine);            sb.Append("             return \"Hello world!\";");            sb.Append(Environment.NewLine);            sb.Append("        }");            sb.Append(Environment.NewLine);            sb.Append("    }");            sb.Append(Environment.NewLine);            sb.Append("}");            string code = sb.ToString();            Console.WriteLine(code);            Console.WriteLine();            return code;        }    }}

C#動態編譯

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.