代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CSharp;
using System.IO;
using System.CodeDom.Compiler;
namespace CompileFiles
{
class Program
{
static void Main(string[] args)
{
//*************************
IDictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("CompilerVersion", "v3.5");
CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider(dic);
CompilerParameters paras = new CompilerParameters();
paras.GenerateExecutable = false; //编译成exe还是dll
//paras.ReferencedAssemblies.Add("System.dll");
paras.GenerateInMemory = true; //是否写入内存,不写入内存就写入磁盘
paras.OutputAssembly = "C:\\UserInfo.dll"; //输出路径
paras.ReferencedAssemblies.Add("System.dll");
paras.ReferencedAssemblies.Add(@"C:\WINDOWS\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll");
StreamReader sr = new StreamReader(@"C:\UserInfo.cs");
String txtStr = sr.ReadToEnd();
CompilerResults result = objCSharpCodePrivoder.CompileAssemblyFromSource(paras, txtStr);
if (result.Errors.HasErrors)
{
string ErrorMessage = "";
foreach (CompilerError err in result.Errors)
{
ErrorMessage += err.ErrorText;
}
Console.WriteLine(ErrorMessage);
}
//*************************
Console.ReadKey();
}
}
}
// -----------------------------------------
using System;
namespace Model
{
public class UserInfo
{
public virtual int ID { get; set; }
public virtual string UserID { get; set; }
public virtual string UserName { get; set; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CSharp;
using System.IO;
using System.CodeDom.Compiler;
namespace CompileFiles
{
class Program
{
static void Main(string[] args)
{
//*************************
IDictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("CompilerVersion", "v3.5");
CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider(dic);
CompilerParameters paras = new CompilerParameters();
paras.GenerateExecutable = false; //编译成exe还是dll
//paras.ReferencedAssemblies.Add("System.dll");
paras.GenerateInMemory = true; //是否写入内存,不写入内存就写入磁盘
paras.OutputAssembly = "C:\\UserInfo.dll"; //输出路径
paras.ReferencedAssemblies.Add("System.dll");
paras.ReferencedAssemblies.Add(@"C:\WINDOWS\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.dll");
StreamReader sr = new StreamReader(@"C:\UserInfo.cs");
String txtStr = sr.ReadToEnd();
CompilerResults result = objCSharpCodePrivoder.CompileAssemblyFromSource(paras, txtStr);
if (result.Errors.HasErrors)
{
string ErrorMessage = "";
foreach (CompilerError err in result.Errors)
{
ErrorMessage += err.ErrorText;
}
Console.WriteLine(ErrorMessage);
}
//*************************
Console.ReadKey();
}
}
}
// -----------------------------------------
using System;
namespace Model
{
public class UserInfo
{
public virtual int ID { get; set; }
public virtual string UserID { get; set; }
public virtual string UserName { get; set; }
}
}