(これにふさわしいタイトルを見つけるのに苦労しています)
それぞれクラスを含むいくつかのファイルをコンパイルするには、C# CodeDomProvider を使用する必要があります。各ファイルを処理するために呼び出される上記のプロバイダーを含む「ジェネレーター」クラスを作成しました。ここにコード全体を貼り付けることはできませんが、関連する部分は次のとおりです。
private Assembly CompileFile(string _file)
{
CodeDom provider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters cparams = new CompilerParameters();
cparams.GenerateExecutable = false;
cparams.GenerateInMemory = true;
cparams.ReferencedAssemblies.Add (/*assembly containing attributes classes */ assemblyReference);
cparams.OutputAssembly = "outputAssembly"; // Required to be able to compile more than one file with the same provider, for some reason
CompilerResults results = provider.CompileAssemblyFromFile(_file);
if (results.Errors.HasErrors)
{
StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
foreach (CompilerError error in results.Errors )
{
errors.AppendFormat("Line {0},{1}\t: {2}\n", error.Line, error.Column, error.ErrorText);
}
throw new Exception(errors.ToString());
}
return results.CompiledAssembly;
}
そして別の方法で:
Assembly asm = CompileFile(_file);
foreach (Type t in asm.GetTypes())
Console.WriteLine (t.ToString ());
このコードが同じジェネレーター クラス内で 2 回呼び出された場合、asm.GetTypes() はコンパイルされた最初の型のみを返し、2 番目の型は返しません。また、プロバイダーとコンパイルパラメーターを同じ結果で再インスタンス化しようとしました。また、ジェネレータークラス全体を再インスタンス化しようとしましたが、これも同じ問題です。コンパイル中に例外はスローされません。
何が間違っている可能性がありますか?