Tôi đang cố gắng biên dịch một assembly từ mã của tôi với nhà cung cấp mã C#.Lỗi lắp ráp. Cố gắng tải lắp ráp được biên dịch với nhà cung cấp mã C#
Khi tôi truy cập assembly đã biên dịch với compilerResult.CompiledAssembly, mọi thứ đều hoạt động. Tuy nhiên, khi tôi thay vì làm Assembly.Load (đường dẫn), tôi nhận được ngoại lệ sau đây:
System.IO.FileLoadException: Không thể tải tập tin hoặc lắp ráp 'C: \ Users \ Tên \ Desktop \ đầu ra. dll 'hoặc một trong những phụ thuộc của nó. tên lắp ráp hoặc mã codebase không hợp lệ. (Ngoại lệ từ HRESULT: 0x80131047)
Tôi đang làm gì sai?
Dưới đây là các mã:
[Test]
public static void CompileCodeIntoAssembly()
{
var code = "public class X { }";
var file = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "output.cs");
File.WriteAllText(file, code);
using (var provider = new CSharpCodeProvider())
{
var parameters = new CompilerParameters
{
GenerateInMemory = false, // we want the dll saved to disk
GenerateExecutable = false,
CompilerOptions = "/target:library /lib:\"" + typeof(Class2).Assembly.Location + "\"",
OutputAssembly = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "output.dll"),
};
parameters.ReferencedAssemblies.AddRange(new[]
{
"System.dll",
typeof(Class1).Assembly.Location,
});
var compilerResult = provider.CompileAssemblyFromFile(parameters, file);
if (compilerResult.Errors.Count > 0)
{
compilerResult.Errors.Cast<object>().ToDelimitedString(Environment.NewLine).Dump();
throw new Exception();
}
var assembly = Assembly.Load(parameters.OutputAssembly);
//var assembly = compilerResult.CompiledAssembly; // this method works
var type = assembly.GetTypes().Single(t => t.Name == "X");
}
1 - Suy nghĩ đầu tiên của tôi là nó có thể là một AnyCPU vs x86 vs vấn đề x64, nhưng sau khi đọc qua MSDN, bạn là đúng! – Pondidum