CSharpCodeProvider를 사용하여 코드를 동적으로 컴파일하려고합니다. 참조 된 어셈블리에서 typeof (Program) .Assembly.CodeBase에 대한 참조 매개 변수를 추가하고 있는데, 이는 here으로 제안되었지만 작동하지 않습니다. 그렇지 않으면 오류 메시지에서 파일 이름을 파일 확장자가 파일 탐색기 (".DLL")에 소문자를 표시하는 유일한 차이는 있지만 - 나는 아직도 그 이름의 파일이 존재CSharpCodeProvider 현재 어셈블리를 참조하는 방법
error CS0006: Metadata file 'file:///C:/Code/MyProject/bin/MyProject.DLL' could not be found;
말하는 오류가 참조하려는 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() });
}
}
'.CodeBase' 대신'typeof (Program) .Assembly.Location'을 사용해보십시오. 어셈블리의'.Location' 속성은 곧은 경로를 반환하고,'.CodeBase'는 URI 형태의 위치를 반환합니다. 확실하지는 않지만'.Location'이 아무 것도주지 않고'.CodeBase'가 제공 할 수있는 원격 호스팅 코드를로드하는 것과 관련된 시나리오가 있다고 생각합니다. 'http' URI이지만 시나리오에서는 어셈블리가 항상 로컬이 될 것 같은데, 그래서 항상 유효한'.Location' 값을 가져야합니다. :-) –
완료 및 감사 :-) –
감사! 그게 효과가있어 - 대답으로 추가하면 .CodeBase와는 아주 다른 경로를 찾았습니다 - CodeBase는 bin 폴더 위치를 가져 왔고 Location에는 임시 디렉토리가 있습니다 : C : \\ Windows \\ Microsoft.NET \\ Framework64 \\ v4.0.30319 \\ 임시 ASP.NET 파일 \\ myIISDirName \\ 65307448 \\ ec575f43 \\ 어셈블리 \\ dl3 \\ d46288d9 \\ 3e71eb5a_c275d201 \\ MyProject.dll. 이 게시물 (http://stackoverflow.com/questions/864484/getting-the-path-of-the-current-assembly)은 .Location이 섀도 복사하기 전에 주소를 가져오고, 약간 혼란스러워하게하는 것으로 보인다. 위치가 안전 할 때 ... – user756366