简体   繁体   English

如何在 memory 中编译并使用 Microsoft.CodeAnalysis 获取程序集

[英]How to compile in memory and get assembly using Microsoft.CodeAnalysis

Before I was using System.CodeDom.Compiler.在我使用 System.CodeDom.Compiler 之前。 It has options that allow compiling in memory and return assembly:它具有允许在 memory 中编译并返回程序集的选项:

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.我正在迁移到 .net 核心,我正在使用 Microsoft.CodeAnalysis 从字符串进行编译。 But I couldn't find 'compile in memory' function on it.但是我在上面找不到'compile in memory' function。 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以下代码对我有用

 Assembly assembly = null;
        using (var memoryStream = new MemoryStream())
        {
            var emitResult = compilation.Emit(memoryStream);
            if (emitResult.Success)
            {
                memoryStream.Seek(0, SeekOrigin.Begin);

                var assemblyLoadContext = AssemblyLoadContext.Default;

                assembly = assemblyLoadContext.Assemblies.FirstOrDefault(a => a.GetName()?.Name == AssemblyName);
                if (assembly == null)
                {
                    assembly = assemblyLoadContext.LoadFromStream(memoryStream);
                }
            }
        }

        return assembly;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法加载文件或程序集 Microsoft.CodeAnalysis - Could not load file or assembly Microsoft.CodeAnalysis 使用 Microsoft.CodeAnalysis,如何获取 Type 类型的 Attribute 属性的值? - Using Microsoft.CodeAnalysis, how do I get the value of an Attribute property that is of type Type? 如何使用 Microsoft.CodeAnalysis(或类似)获取项目(.csproj)的目标框架? - How to get the Target Framework/s of a project (.csproj) using Microsoft.CodeAnalysis (or similar)? 使用Roslyn(Microsoft.CodeAnalysis)查询WebSite项目的信息 - Using Roslyn (Microsoft.CodeAnalysis) to query information of WebSite projects PowerShell 7.0.x 无法加载文件或程序集 Microsoft.CodeAnalysis - PowerShell 7.0.x Could not load file or assembly Microsoft.CodeAnalysis 无法加载文件或程序集 'Microsoft.CodeAnalysis,版本 = 3.11.0.0,文化 = 中性 - Could not load file or assembly 'Microsoft.CodeAnalysis, Version=3.11.0.0, Culture=neutral 警告 CS8032 无法加载文件或程序集 'Microsoft.CodeAnalysis,版本 = 3.3.0.0 - Warning CS8032 Could not load file or assembly 'Microsoft.CodeAnalysis, Version=3.3.0.0 C#vS2017项目未生成:无法加载文件或程序集Microsoft.CodeAnalysis - C# vS2017 project not building: Could not load file or assembly Microsoft.CodeAnalysis 在Roslyn与Microsoft.CodeAnalysis中添加MetadataReference - Adding MetadataReference in Roslyn Vs Microsoft.CodeAnalysis Microsoft.CodeAnalysis无法加载文件 - Microsoft.CodeAnalysis failed to load file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM