CSharpCodeProvider を使用してコードを動的にコンパイルしようとしています。参照されているアセンブリでは、ここで提案されているように、typeof(Program).Assembly.CodeBase) の参照パラメーターを追加していますが、機能しません。まだエラーが表示されます
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() });
}
}