2012-05-01 1 views
6

私はこれとどこに行くのかは分かりません。全体的な目的は、ユーザースクリプトを.NET環境内で実行できるようにすることです。私は自分のアセンブリを読み込もうとしない限り、ほとんどのコードを書いています。ただし、ユーザーがシステムの内部に安全にアクセスできるようにするために、プロキシDLLが作成されています。これが問題のように見えます。CompileAssemblyFromSourceを使用してカスタムアセンブリをロードする

今、このプロキシDLLには、インターフェイスが1つあります。もちろん、

System.IO.FileNotFoundException : Could not load file or assembly 'file:///C:\Users...\AppData\Local\Temp\scts5w5o.dll' or one of its dependencies. The system cannot find the file specified.

私の最初の考えはある "...とscts5w5o.dllは何ですか?":上記のコードを実行する

CompilerParameters options = new CompilerParameters(); 
options.GenerateExecutable = false; 
options.GenerateInMemory = true; 
options.ReferencedAssemblies.Add("System.dll"); 
options.ReferencedAssemblies.Add("ScriptProxy.dll"); 

Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider(); 
CompilerResults result = provider.CompileAssemblyFromSource(options, script); 

// This line here throws the error: 
return result.CompiledAssembly; 

、それは次のようなエラーがスローされます

これはScriptProxy.dllが正しく読み込まれていないのですか、ScriptProxy.dll自体が依存ファイルをロードしようとしていますか?それとも全く違うものなのでしょうか?

私は、このコードをNUnitテストランナーから実行しています。違いがあるかどうかは分かりません。

答えて

7

コンパイル手順が失敗し、あなたがエラーのためにそれをチェックしていないので、これはある...

static Assembly Compile(string script) 
    { 
     CompilerParameters options = new CompilerParameters(); 
     options.GenerateExecutable = false; 
     options.GenerateInMemory = true; 
     options.ReferencedAssemblies.Add("System.dll"); 
     options.ReferencedAssemblies.Add("ScriptProxy.dll"); 

     Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider(); 
     CompilerResults result = provider.CompileAssemblyFromSource(options, script); 

     // Check the compiler results for errors 
     StringWriter sw = new StringWriter(); 
     foreach (CompilerError ce in result.Errors) 
     { 
      if (ce.IsWarning) continue; 
      sw.WriteLine("{0}({1},{2}: error {3}: {4}", ce.FileName, ce.Line, ce.Column, ce.ErrorNumber, ce.ErrorText); 
     } 
     // If there were errors, raise an exception... 
     string errorText = sw.ToString(); 
     if (errorText.Length > 0) 
      throw new ApplicationException(errorText); 

     return result.CompiledAssembly; 
    } 
+0

これは意味があります。私は今これを試してみるでしょう。 –

+1

私は答えとしてマークするつもりです。なぜなら、それは問題ごとに解決しなかったものの、実際のエラーメッセージを得るのに役立ちました。 –

+0

どうすれば実際の問題を解決できますか? – Moumit

3

私はanswerとしてマークされたポストが本当に答えではないと思います! ...しかし、私はそれはあなたが(いつか.NETのDLLも例外を与える)第三者いるdllの参照を追加しようとした場合、単純executable folderでそれをコピーし...それが正常に動作することを意味しhere

parameters.ReferencedAssemblies.Add(typeof(<TYPE FROM DOMAIN.DLL>).Assembly.Location); 

答えを見つけました..それ以外の場合は、フルパスを定義することもできます。