C#コードプロバイダを使用してアセンブリからアセンブリをコンパイルしようとしています。C#コードプロバイダでコンパイルされたアセンブリをロードしようとすると奇妙なAssembly.Loadエラーが発生する
compilerResult.CompiledAssemblyでコンパイルされたアセンブリにアクセスすると、すべて動作します。
System.IO.FileLoadException::私が代わりにAssembly.Load(パス)を行うときにしかし、私は次の例外を取得\ Users \ユーザー名\デスクトップ\出力:ファイルまたはアセンブリ 」Cを読み込むことができませんでした。 dll 'またはその依存関係の1つ。指定されたアセンブリ名またはコードベースの が無効でした。 (HRESULTからの例外: 0x80131047)
私は間違っていますか?
[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のように、アセンブリ名を期待 - 私の最初に考えたのは、それはx64の問題対x86の対AnyCPU可能性がありましたが、MSDNを通じて読んだ、あなたは正しいです! – Pondidum