.Net2.0在程序中编译代码

using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.CSharp;
using System.CodeDom.Compiler;

namespace CodeCompiler_Example
{
    
class Program
    {
        
static string strCsCode = @"
            class Cls
            {
                static void Main(string[] args)
                {
                    global::System.Console.WriteLine(""HelloWorld!"");
                    global::System.Console.Read();
                }
            }
        
";
        
static void Main(string[] args)
        {
            
try
            {
                CSharpCodeProvider provider 
= new CSharpCodeProvider();
                
// ICodeCompiler compiler = provider.CreateCompiler(); // For .Net FW 1.1
                CompilerParameters parame = new CompilerParameters();
                parame.OutputAssembly 
= @"C:\Cls.exe";
                parame.GenerateExecutable 
= true;

                provider.CompileAssemblyFromSource(parame, strCsCode);
            }
            
catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine(
"Done.");
            Console.Read();
        }
    }
}

posted on 2006-07-27 18:13  萝卜青菜  阅读(414)  评论(1编辑  收藏  举报

导航