public static System.Reflection.Assembly CreateAssembly(string ClassNameSpace,string ClassName,string ClassSourceCode,string[] ReferencedDll,string ResFileName,string OutputDllFileName)
{
// 设置编译参数。
System.CodeDom.Compiler.CompilerParameters paras = null;
paras = new CompilerParameters();
paras.GenerateInMemory = true;
paras.GenerateExecutable = false;
paras.TreatWarningsAsErrors = false;
paras.WarningLevel = 4;
paras.CompilerOptions=ResFileName;//@"/resource:Users.hbm.xml";//加入文件做嵌入的资源
//引用的DLL
paras.ReferencedAssemblies.AddRange(ReferencedDll);//includes
//输出得程序集即文件名
if (OutputDllFileName==""||OutputDllFileName==null)
paras.OutputAssembly=ClassNameSpace+"."+ClassName+".dll";
else
paras.OutputAssembly = OutputDllFileName+".dll";
// 创建动态代码。
// 创建编译器实例。
System.CodeDom.Compiler.ICodeCompiler compiler = null;
compiler = new Microsoft.CSharp.CSharpCodeProvider().CreateCompiler();
// 编译代码。
System.CodeDom.Compiler.CompilerResults result = null;
result = compiler.CompileAssemblyFromSource(paras,ClassSourceCode);
if (result.Errors.Count>0)
{
string s=null;
foreach(System.CodeDom.Compiler.CompilerError err in result.Errors)
{
s+=(err.ToString())+"/n";
}
throw new System.Exception(s);
}
// 获取编译后的程序集。
System.Reflection.Assembly assembly = result.CompiledAssembly;
// 动态调用方法。
//object reObject=assembly.CreateInstance(ClassNameSpace+"."+ClassName);
return assembly;
}