我想我有一个可行的解决方案。感谢大家为我指明了正确的方向(我希望如此)。
程序集不能直接卸载,但 AppDomain 可以。我创建了一个帮助程序库,该库加载到新的 AppDomain 中,并且能够从代码编译新的程序集。该帮助程序库中的类如下所示:
public class CompilerRunner : MarshalByRefObject
{
private Assembly assembly = null;
public void PrintDomain()
{
Console.WriteLine("Object is executing in AppDomain \"{0}\"",
AppDomain.CurrentDomain.FriendlyName);
}
public bool Compile(string code)
{
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
parameters.GenerateExecutable = false;
parameters.ReferencedAssemblies.Add("system.dll");
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, code);
if (!results.Errors.HasErrors)
{
this.assembly = results.CompiledAssembly;
}
else
{
this.assembly = null;
}
return this.assembly != null;
}
public object Run(string typeName, string methodName, object[] args)
{
Type type = this.assembly.GetType(typeName);
return type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, assembly, args);
}
}
这是非常基本的,但足以进行测试。 PrintDomain 可以验证它是否存在于我的新 AppDomain 中。编译需要一些源代码并尝试创建一个程序集。 Run 让我们测试给定源代码中静态方法的执行情况。
以下是我使用辅助库的方法:
static void CreateCompileAndRun()
{
AppDomain domain = AppDomain.CreateDomain("MyDomain");
CompilerRunner cr = (CompilerRunner)domain.CreateInstanceFromAndUnwrap("CompilerRunner.dll", "AppDomainCompiler.CompilerRunner");
cr.Compile("public class Hello { public static string Say() { return \"hello\"; } }");
string result = (string)cr.Run("Hello", "Say", new object[0]);
AppDomain.Unload(domain);
}
它基本上创建域,创建我的帮助器类(CompilerRunner)的实例,使用它编译新程序集(隐藏),从该新程序集运行一些代码,然后卸载域以释放内存。
您会注意到 MarshalByRefObject 和 CreateInstanceFromAndUnwrap 的使用。这些对于确保帮助程序库确实存在于新域中非常重要。
如果有人注意到任何问题或有改进的建议,我很乐意听到他们的声音。