I am trying to write a Blazor WebAssembly (WASM) app that accepts some code (from some text input field) and compiles the code using Roslyn.
I'm using Roslyn's CSharpCompilation
class to create the compilation. Its Create
method takes four parameters, one of which is a list of MetadataReference
s (aka assembly references). In other (non-blazor) type applications, like a C# console app, you could get these MetadataReference
s based on Asssembly Location, like this:
var locatedAssemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => !string.IsNullOrEmpty(a.Location)).ToArray();
foreach (var assembly in locatedAssemblies)
{
MetadataReference reference = MetadataReference.CreateFromFile(assembly.Location);
}
This unfortunately no longer works in Blazor WASM, because the Location
s of the assemblies are empty.
I had tried getting assemblies in different ways, like AppDomain.CurrentDomain.GetAssemblies()
and Assembly.GetEntryAssembly().GetReferencedAssemblies()
, but all had empty Location
s. I also tried calling Assembly.Load()
, but to no avail.
Does anyone know how to get MetadataReference
s in Blazor WASM, or how I would otherwise create a compilation in Blazor WASM?
(I'm also aware of MetadataReference.CreateFromStream()
that I'll probably need to use, but it still requires the assembly location).
Thanks in advance.