【发布时间】:2015-01-27 12:35:34
【问题描述】:
我在VS2013中有一个解决方案,该解决方案有2个项目,projA和projB。
在启动项目 projA 中,我编写了一些代码来在运行时编译 C# 文本文件,当将其链接到运行它的 projA 内的命名空间时,我的代码实际上运行良好。
但是当我尝试将它链接到 projB 内部的命名空间时,例如执行 using namespaceInProjB 之类的操作,它说它们不存在。我认为它们也会在当前的域程序集中找到?
有谁知道如何将它们也包括在内?这是我的代码:
CodeDomProvider provider = new CSharpCodeProvider();
CompilerParameters compilerParams = new CompilerParameters();
compilerParams.CompilerOptions = "/target:library";
compilerParams.GenerateExecutable = false;
compilerParams.GenerateInMemory = true;
compilerParams.IncludeDebugInformation = false;
compilerParams.ReferencedAssemblies.Add("mscorlib.dll");
var assemblies = AppDomain.CurrentDomain
.GetAssemblies()
.Where(a => !a.IsDynamic)
.Select(a => a.Location);
foreach (string str in assemblies.ToArray<string>())
compilerParams.ReferencedAssemblies.Add(str);
CompilerResults result = provider.CompileAssemblyFromSource(compilerParams, scriptCode);
// Write out all the errors
if (result.Errors.Count > 0)
{
errors = result.Errors;
return null;
}
return result.CompiledAssembly;
【问题讨论】:
标签: c# compilation runtime