假设 ProjectMain
有对 Project 的引用Ref
。在Main
中,我定义了一个CSharpCodeProvider
并使用它在运行时编译代码。
var provider = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
var parameters = new CompilerParameters();
parameters.ReferencedAssemblies.Add("System.dll");
// Rest of the referenced assemblies.
在运行时编译的代码可能需要更新版本的 ProjectRef
才能正确运行。所以我尝试Ref.Dll
在相对子文件夹(plugins
)中添加新的:
parameters.ReferencedAssemblies.Add(@"d:\project-output-path\plugins\Ref.dll");
我还添加了以下内容:
AppDomain.CurrentDomain.AppendPrivatePath("plugins");
问题是当我尝试动态编译脚本时,Ref.dll
正在使用主文件夹中的并导致错误。
Ref
那么,仅针对我的脚本引用新项目的最佳方式是什么?
PS 我真的更喜欢不必创建另一个 AppDomain,因为动态执行的代码与当前 AppDomain 中加载的代码相耦合,无法分离。