【发布时间】:2016-03-12 19:06:15
【问题描述】:
我想为脚本编写一个可重新加载的汇编函数。(这样我可以更快地调试脚本)
dll 生成也可以正常加载。主要问题是,我在我的主 AppDomain 中使用我的临时 AppDomain 的功能。该 dll 似乎也链接到我的主 AppDomain,因为在程序运行时我无法删除它。 如果我从我的主 AppDomain“上下文”中删除所有 MethodInfo 引用,那么删除它就没有问题。
在这里你可以看到程序是如何工作的:
- 从外部进程生成 DLL
- 通过 (temp AppDomain).DoCallBack(...) 加载 DLL
- 获取 Type & MethodInfo 并调用它。
- AppDomain.Unload(temp AppDomain)
因此,如果我跳过第 3 步,删除 dll 就没有问题。但我无法检查函数的返回是否真的显示了更新的值(我在脚本中编辑)。
我在这里发布了每个步骤的源代码: 1.
//This actually isn`t as important for you
Process assemblyGeneration = new Process();
assemblyGeneration.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + @"lib\AssemblyCompiler.exe";
assemblyGeneration.StartInfo.Arguments = "\"" + AppDomain.CurrentDomain.BaseDirectory + "script.dll\" \"" + source + "\" \"System.dll\"";
assemblyGeneration.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
assemblyGeneration.StartInfo.UseShellExecute = true;
assemblyGeneration.Start();
assemblyGeneration.WaitForExit();
assemblyGeneration.Dispose();
2.
_appDomain.DoCallBack(() =>
{
byte[] fileContent = File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + "script.dll");
AppDomain.CurrentDomain.Load(fileContent);
});
3.
MethodInfo info = _compiledScript.GetTypeFrom("LSCT.ScriptClass").GetMethod("DefineX");
var func = (Func<double>) Delegate.CreateDelegate(typeof(Func<double>), info);
double x = func();
Console.WriteLine("Output was : " + x);
4.
AppDomain.Unload(_appDomain);
那么有没有办法解决我无法重新加载 DLL 的问题?
【问题讨论】: