2
ローズインで生成されたアセンブリをバイトの配列に入れて、それらのバイトから別のアセンブリにロードしたいと思っています。ネットフレームワーク4.6で動作しますが、 .NET COREで失敗するASP.NET COREのバイトからアセンブリをロード
これはコードです。ネットコアで
public class Pr2
{
public static void Main()
{
var code = File.ReadAllText(@"E:\VictoriaCMS\WebApplication4\WebApplication4\Controllers\code.cs");
var tree = SyntaxFactory.ParseSyntaxTree(code);
var compilation = CreateCompilation(tree);
var model = compilation.GetSemanticModel(tree);
ShowLocalDeclarations(tree, model);
ShowDiagnostics(compilation);
ExecuteCode(compilation);
}
private static void ShowLocalDeclarations(SyntaxTree tree, SemanticModel model)
{
var locals = tree.GetRoot()
.DescendantNodes()
.OfType<LocalDeclarationStatementSyntax>();
foreach (var node in locals)
{
var type = model.GetTypeInfo(node.Declaration.Type);
Console.WriteLine("{0} {1}", type.Type, node.Declaration);
}
}
private static void ExecuteCode(CSharpCompilation compilation)
{
using (var stream = new MemoryStream())
{
compilation.Emit(stream);
var assembly = Assembly.Load(stream.GetBuffer());
var type = assembly.GetType("Greeter");
var greeter = Activator.CreateInstance(type);
var method = type.GetMethod("SayHello");
method.Invoke(greeter, null);
}
}
private static void ShowDiagnostics(CSharpCompilation compilation)
{
var diagnostics = compilation.GetDiagnostics();
foreach (var diagnostic in diagnostics)
{
Console.WriteLine(diagnostic.ToString());
}
}
private static CSharpCompilation CreateCompilation(SyntaxTree tree)
{
var options = new CSharpCompilationOptions(
OutputKind.DynamicallyLinkedLibrary);
var reference = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
var compilation =
CSharpCompilation.Create("test")
.WithOptions(options)
.AddSyntaxTrees(tree)
.AddReferences(reference);
return compilation;
}
}
私はいくつかのreason.Canの誰もが私を助けるためAssembly.Load(バイト[]バッファ)の過負荷を持っていませんか?ありがとうございました。
var assembly = Assembly.Load(stream.GetBuffer());
あなたは
AssemblyLoadContext
を使用して、.NETのコア内のストリームからアセンブリをロードすることができ