2009-02-26 5 views
5

実行時に動的アセンブリをコンパイルしています。別のDLLを参照する必要があります。 CompilerParametersでOutputAssemblyを設定する限り、すべて正常に機能します。しかし、私がGenerateInMemory = trueを設定するとすぐに。それは失敗します:実行時にクラスをコンパイルすると、CompilerParameters.GenerateInMemory == trueの場合に失敗します。

var compilerParameters = new CompilerParameters(); 
if(compileInMemory) 
    compilerParameters.GenerateInMemory = true; 
else 
    compilerParameters.OutputAssembly = "<my_dynamic_dll_path>"; 
compilerParameters.ReferencedAssemblies.Add("<other_dll_path>"); 
var compilerResults = new CSharpCodeProvider().CompileAssemblyFromDom(compilerParameters, codeCompileUnit); 

// Here: compilerResults.Errors.HasErrors == false 

foreach(var type in compilerResults.CompiledAssembly.GetTypes()) 
{ 
    // Exception: 
    // Unable to load one or more of the requested types. 
    // Retrieve the LoaderExceptions property for more information. 
} 

LoaderExceptionsは "other_dll"が見つかりませんでした。メモリ内でコンパイルしない限り動作するのはなぜですか?また、メモリ内でコンパイルするにはどうすればいいですか?

+0

また、other.dllへのパスをハードコードするとしますか? –

+0

申し訳ありませんが、パスはすでにハードコードされています - 私の投稿を編集して明確にします – tanascius

答えて

5

GenerateInMemoryを使用すると読み込みコンテキストがなく、Assembly.Load(Byte [])オーバーロードによってアセンブリが読み込まれます。 1つの回避策は、AppDomain.AssemblyResolveイベントを一時的にフックして、 "other_dll"を自分自身で読み込むことです。

+0

ありがとうございます。提案されたイベントは、私が問題を解決するのに役立ちます。 – tanascius

+0

こんにちは、私は同じ問題がありますが、提案されたイベントがどのように問題を解決するのに役立つか理解できません。表示してください。 ありがとう –

関連する問題