I am experiencing with Roslyn on a .NET Core console application. I tried to create an assembly and compile it into a memory stream. But loading it seems to be missing an API (that normally works on the regular .NET Framework)
var compilation = CSharpCompilation.Create("MyCompilation",
syntaxTrees: new[] { pocoTree }, references: new[] { mscorlib, currentAssembly });
var ms = new MemoryStream();
var emitResult = compilation.Emit(ms);
var ourAssembly = Assembly.Load(ms.ToArray()); // Fails to compile here
I get this error:
cannot convert from 'byte[]' to 'System.Reflection.AssemblyName'
What is the alternative in .NET Core to load from a steam? (other than physically saving the assembly and loading it)
You can load the assembly from a stream in .NET core using AssemblyLoadContext
:
// requires "System.Runtime.Loader": "4.0.0",
protected virtual Assembly LoadAssembly(MemoryStream peStream, MemoryStream pdbStream)
{
return System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromStream(peStream, pdbStream);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With