2012-05-01 2 views
6

어디로 갈 지 잘 모르겠습니다. 전반적인 목적은 사용자 스크립트를 가져 와서 .NET 환경 내에서 실행할 수있게하는 것입니다. 필자는 자신의 어셈블리를로드하려고 시도하지 않으면 대부분의 코드를 작성하고 작동합니다. 그러나 사용자에게 시스템의 내부 부분에 대한 액세스 권한을 안전하게 부여하기 위해 프록시 DLL이 만들어졌습니다. 이것은 문제가있는 곳입니다.CompileAssemblyFromSource를 사용하여 사용자 지정 어셈블리로드

지금이 프록시 DLL에는 인터페이스가 하나 있습니다. 물론

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로 표시된 게시물을 생각하지 않는다 정말 대답입니다! ...하지만 난 .. 그것을 잘 작동합니다 당신은 단순히 executable folder에 복사 (언젠가 .NET DLL을 또한 예외를 부여) 제 3 자입니다 dll 참조를 추가하려고하면 그것은 의미 here

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

답을 발견 .. 다른 곳에서 전체 경로를 정의 할 수도 있습니다 ..

관련 문제