C# 代码编译器 及 编译器实例

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

// 备注:若返回值是字符串类型 则 需要 转义,不然 代码编译出错

 

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.CSharp;
using System.Text;
using System.Reflection;

namespace CodeCompiler_Demo
{
    public class Class
    {
        public static object GetObject(string strGreet)
        {
            CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");

            System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
            parameters.ReferencedAssemblies.Add("System.dll");
            parameters.GenerateExecutable = false;
            parameters.GenerateInMemory = true;

            string sourceCode = string.Empty;
            StringBuilder sb = new StringBuilder();
            sb.Append("using System;");
            sb.Append(Environment.NewLine);
            sb.Append("namespace CodeCompiler_Demo");
            sb.Append(Environment.NewLine);
            sb.Append("{");
            sb.Append(Environment.NewLine);
            sb.Append("     public class Greet");
            sb.Append(Environment.NewLine);
            sb.Append("     {");
            sb.Append(Environment.NewLine);
            sb.Append("         public string GetGreet()");
            sb.Append(Environment.NewLine);
            sb.Append("         {");
            sb.Append(Environment.NewLine);
            sb.Append("              return \"" + strGreet + "\";");
            sb.Append(Environment.NewLine);
            sb.Append("         }");
            sb.Append(Environment.NewLine);
            sb.Append("     }");
            sb.Append(Environment.NewLine);
            sb.Append("}");

            sourceCode = sb.ToString();

            CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, sourceCode);
            object obj = null;
            if (!results.Errors.HasErrors)
            {
                object entity = results.CompiledAssembly.CreateInstance("CodeCompiler_Demo.Greet");
                MethodInfo mi = entity.GetType().GetMethod("GetGreet");
                obj = mi.Invoke(entity, null);
            }

            return obj;

        }

        public static object GetEntity()
        {
            // 1.CSharpCodePrivoder
            CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();

            // 2.ICodeComplier
            ICodeCompiler objICodeCompiler = objCSharpCodePrivoder.CreateCompiler();

            // 3.CompilerParameters
            CompilerParameters objCompilerParameters = new CompilerParameters();
            objCompilerParameters.ReferencedAssemblies.Add("System.dll");
            objCompilerParameters.GenerateExecutable = false;
            objCompilerParameters.GenerateInMemory = true;

            // 4.CompilerResults
            CompilerResults cr = objICodeCompiler.CompileAssemblyFromSource(objCompilerParameters, GenerateCode());

            object obj = null;
            if (cr.Errors.HasErrors)
            {

            }
            else
            {
                // 通过反射,调用HelloWorld的实例
                Assembly objAssembly = cr.CompiledAssembly;
                object objHelloWorld = objAssembly.CreateInstance("DynamicCodeGenerate.HelloWorld");
                MethodInfo objMI = objHelloWorld.GetType().GetMethod("OutPut");

                obj = objMI.Invoke(objHelloWorld, null);
            }

            return obj;

        }

        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();

            return code;
        }
    }
}

转载于:https://my.oschina.net/guanxinsui/blog/956581

你可能感兴趣的:(C# 代码编译器 及 编译器实例)