关于动态编译的问题(为啥生成的文件最后删除不掉)

angeldgz angeldgz 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
上代码:
 public static T Eval<T>(string code)
{
//get the code to compile
string strSourceCode = GetComplainCode(typeof(T).Name,code);
//1. Create an addtional AppDomain
AppDomainSetup objSetup = new AppDomainSetup();
objSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
AppDomain objAppDomain = AppDomain.CreateDomain("MyAppDomain", null, objSetup);
// 1.Create a new CSharpCodePrivoder instance
CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
// 2.Sets the runtime compiling parameters by crating a new CompilerParameters instance
CompilerParameters objCompilerParameters = new CompilerParameters();
objCompilerParameters.ReferencedAssemblies.Add("system.dll");
objCompilerParameters.ReferencedAssemblies.Add("gzLib.dll");
// Load the remote loader interface
//objCompilerParameters.ReferencedAssemblies.Add("RemoteAccess.dll");
// Load the resulting assembly into memory
objCompilerParameters.GenerateInMemory = false;
string outFileName = "TempCompilerResult" + new Random().Next() + ".dll";
objCompilerParameters.OutputAssembly = outFileName;
// 3.CompilerResults: Complile the code snippet by calling a method from the provider
CompilerResults cr = objCSharpCodePrivoder.CompileAssemblyFromSource(objCompilerParameters, strSourceCode);
if (cr.Errors.HasErrors) {
StringBuilder error = new StringBuilder();
error.Append("Error Compiling Expression: ");
foreach (CompilerError err in cr.Errors)
{
error.AppendFormat("{0}\n", err.ErrorText);
}
throw new Exception("Error Compiling Expression: " + error.ToString());
}
// 4. Invoke the method by using Reflection
object evalObj=objAppDomain.CreateInstance(cr.CompiledAssembly.FullName, "GzAutoEval._GzEvalClass").Unwrap();
// with help of factory, create a real 'LiveClass' instance
MethodInfo mi = evalObj.GetType().GetMethod("Eval");
T value = (T)mi.Invoke(evalObj, null);
AppDomain.Unload(objAppDomain);
System.IO.File.Delete(outFileName);
return value;
}

最后已经卸载AppDomain.Unload(objAppDomain)了,为啥删除输出文件的时候依然出错呢了?
分数不够可以再加,只求大神指点!
...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部
angeldgz
.NET Framework 17,748 成员 24,324 内容