Before I was using System.CodeDom.Compiler. It has options that allow compiling in memory and return assembly:
var compilerParams = new CompilerParameters
{
GenerateInMemory = true,
GenerateExecutable = false,
};
I am migrating to .net core and I am using Microsoft.CodeAnalysis to compile from the string. But I couldn't find 'compile in memory' function on it. My goal is to validate the syntax issue of code. Currently my code is:
var sources = GenerateSourceFromDefaultValue(context, defaultValue, clrType, isEnum);
var parsedSyntaxTree = Parse(sources, "", CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp5));
var compilation = CSharpCompilation.Create("Test.dll", new SyntaxTree[] { parsedSyntaxTree }, options: DefaultCompilationOptions);
The following code worked on me