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;
}
————————————————
版权声明:本文为CSDN博主「酸酸甜甜就是我啊」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/QSWW21CN/article/details/395836
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




