我正在尝试使用CSharpCodeProvider动态编译代码。在引用的程序集中,我为类型(Program).Assembly.CodeBase添加了一个引用参数,这是here的建议,但它不起作用。我还是觉得说错了
error CS0006: Metadata file 'file:///C:/Code/MyProject/bin/MyProject.DLL' could not be found;这个名称的文件确实存在-唯一的区别是文件扩展名在文件资源管理器(".dll")中显示小写,但否则,错误消息中的文件名与我要引用的dll的名称和路径匹配。
知道为什么在这种情况下编译器会看不到引用的dll吗?
下面是我代码的相关部分:
CompilerResults result = null;
CompilerParameters parms = new CompilerParameters();
parms.GenerateExecutable = false;
parms.GenerateInMemory = true;
parms.OutputAssembly = "MyOutputAssembly";
parms.ReferencedAssemblies.Add("System.dll");
parms.ReferencedAssemblies.Add("System.Data.dll");
parms.ReferencedAssemblies.Add("mscorlib.dll");
parms.ReferencedAssemblies.Add(typeof(Program).Assembly.CodeBase); // Reference the current assembly
// Lock because CSharpCodeProvider can only compile the code once per time slot
lock (lockCompile)
{
using (CSharpCodeProvider codeProvider = new CSharpCodeProvider())
{
result = codeProvider.CompileAssemblyFromSource(parms, new string[] { code.ToString() });
}
}发布于 2017-01-23 22:22:36
尝试使用typeof(Program).Assembly.Location而不是.CodeBase。程序集上的.Location属性将向实际加载的文件返回一个直接向上的路径,而.CodeBase则返回URI形式中的规范位置。我不确定,但我认为可能有一些与加载远程托管代码有关的场景,其中.Location不给您任何东西,.CodeBase可能给您提供一个http URI,但是在您的场景中,您的程序集听起来总是本地的,所以您应该始终有一个有效的.Location值。:-)
https://stackoverflow.com/questions/41816011
复制相似问题