在我使用 System.CodeDom.Compiler 之前。它具有允许在内存中编译和返回程序集的选项:
var compilerParams = new CompilerParameters
{
GenerateInMemory = true,
GenerateExecutable = false,
};
我正在迁移到 .net 核心,并且正在使用 Microsoft.CodeAnalysis 从字符串进行编译。但我在上面找不到“在内存中编译”功能。我的目标是验证代码的语法问题。目前我的代码是:
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);