c# 把字符串生成代码并执行

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

namespace Test.Consoles
{
    class Program
    {
        static void Main(string[] args)
        {
            CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");
            CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
            //添加程序集
            CompilerParameters objCompilerParameters = new CompilerParameters();
            objCompilerParameters.ReferencedAssemblies.Add("System.dll");
            /*
             * 第一 要引用要在当前项目中引用Model
             * 第二 在拼接代码的时候也要引用命名空间 using Model;
             */
            objCompilerParameters.ReferencedAssemblies.Add("Model.dll");
            objCompilerParameters.GenerateExecutable = false;//不生成可执行文件
            objCompilerParameters.GenerateInMemory = true;//在内存中输出
            CompilerResults cr;
            //已过时
            // ICodeCompiler objICodeCompiler = objCSharpCodePrivoder.CreateCompiler();
            // cr= objICodeCompiler.CompileAssemblyFromSource(objCompilerParameters, GenerateCode());
            cr = provider.CompileAssemblyFromSource(objCompilerParameters, GenerateCode());
            if (cr.Errors.HasErrors)
            {

                Console.WriteLine("编译错误!");
                foreach (CompilerError err in cr.Errors)
                {
                    Console.WriteLine(err.ErrorText);
                }
            }
            else
            {
                //通过反射,调用hello world 的实例
                Assembly objAssembly = cr.CompiledAssembly;
                Object objHelloWorld = objAssembly.CreateInstance("DynamicCodeGenerate.HelloWorld");
                MethodInfo objMI = objHelloWorld.GetType().GetMethod("getModel");
                Model.SYS_AGENTINFO SYS_AGENTINFO = objMI.Invoke(objHelloWorld, new object[] { "jcdd" }) as Model.SYS_AGENTINFO;
                Console.WriteLine(SYS_AGENTINFO.AGENTNAME);
            }
            Console.ReadLine();
        }
        static string GenerateCode()
        {
            Console.WriteLine(DynamicCodeGenerate.HelloWorld.bbb());

            StringBuilder sb = new StringBuilder();
            sb.Append("using System;");
            sb.Append("using System.Text;");
            sb.Append("using Model;");
            sb.Append("namespace DynamicCodeGenerate");
            sb.Append("{");
            sb.Append(" public class HelloWorld ");
            sb.Append("{");

            sb.Append(" public string OutPut(string value) ");
            sb.Append("{");
            sb.Append(" return \"Hello World!\"+value; ");
            sb.Append("}");

            sb.Append(" public SYS_AGENTINFO getModel(string agentname) ");
            sb.Append("{");
            sb.Append("SYS_AGENTINFO SYS_AGENTINFO = new SYS_AGENTINFO();");
            sb.Append("SYS_AGENTINFO.AGENTNAME=agentname;");
            sb.Append(" Console.WriteLine(\"坐席名称:\"+SYS_AGENTINFO.AGENTNAME);");
            sb.Append("return SYS_AGENTINFO;");
            sb.Append("}");

            sb.Append("}}");

            string code = sb.ToString();
            return code;
        }
    }
}

posted @ 2014-11-03 23:45  JasonGu0  阅读(2104)  评论(0编辑  收藏  举报