简体   繁体   English

Microsoft.CodeDom.Providers.DotNetCompilerPlatform 的问题

[英]Problem with Microsoft.CodeDom.Providers.DotNetCompilerPlatform

I have an application that does some dynamic code generation and compilation and has been working well with System.CodeDom and Microsoft.CSharp.我有一个执行一些动态代码生成和编译的应用程序,并且与 System.CodeDom 和 Microsoft.CSharp 配合得很好。 I am porting this to .net Core 3.1, and so have upgraded to use Microsoft.CodeDom.Providers.DotNetCompilerPlatform.我将其移植到 .net Core 3.1,因此已升级为使用 Microsoft.CodeDom.Providers.DotNetCompilerPlatform。 However, I have run into problems that may be related.但是,我遇到了可能相关的问题。 The first is that Visual Studio displays the following message in the Packages section of References:首先是 Visual Studio 在“参考资料”的“包”部分显示以下消息:

Package 'Microsoft.CodeDom.Providers.DotNetCompilerPlatform 2.0.1' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETCoreApp,Version=v3.1'.使用“.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4”恢复包“Microsoft.CodeDom.Providers.DotNetCompilerPlatform 2.0.1” .7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' 而不是项目目标框架 '.NETCoreApp,Version=v3.1'。 This package may not be fully compatible with your project.此包可能与您的项目不完全兼容。

Secondly (and this may be related), when the application hits this line:其次(这可能是相关的),当应用程序点击这条线时:

CSharpCodeProvider codeProvider = new CSharpCodeProvider();

Then the following exception is thrown:然后抛出以下异常:

The type initializer for 'Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CompilationSettingsHelper' threw an exception. “Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CompilationSettingsHelper”的类型初始值设定项引发异常。

I'd be grateful for any help!我将不胜感激任何帮助!

In the hopes that this may help others trying to solve the same problem, here's what I ended up with using the Microsoft.CodeAnalysis.CSharp package:希望这可以帮助其他人尝试解决同样的问题,这是我最终使用 Microsoft.CodeAnalysis.CSharp 包的结果:

Assembly assembly = null;
string[] sourceStringArray = null;// set this to hold the arrary of source strings
List<SyntaxTree> syntaxTreeList = new List<SyntaxTree>();
foreach (string sourceString in sourceStringArray)
{
    SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(sourceString);
    syntaxTreeList.Add(syntaxTree);
}

string assemblyName = Path.GetRandomFileName();
AppDomain currentDomain = AppDomain.CurrentDomain;
List<MetadataReference> metadataReferenceList = new List<MetadataReference>();
Assembly[] assemblyArray = currentDomain.GetAssemblies();
foreach (Assembly domainAssembly in assemblyArray)
{
    try
    {
        AssemblyMetadata assemblyMetadata = AssemblyMetadata.CreateFromFile(domainAssembly.Location);
        MetadataReference metadataReference = assemblyMetadata.GetReference();
        metadataReferenceList.Add(metadataReference);
    }
    catch (Exception e)
    {
        Log.DebugFormat("failed to get MetadataReference {0}", e.Message);
    }
}

CSharpCompilation compilation = CSharpCompilation.Create(
    assemblyName,
    syntaxTrees: syntaxTreeList,
    references: metadataReferenceList,
    options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
using (var ms = new MemoryStream())
{
    EmitResult result = compilation.Emit(ms);
    if (!result.Success)
    {
        IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error);
        foreach (Diagnostic diagnostic in failures)
        {
            /* Process failures */
        }
    }
    else
    {
        ms.Seek(0, SeekOrigin.Begin);
        assembly = Assembly.Load(ms.ToArray());
    }
}

I am sure that this is not optimal (and would welcome suggestions to improve.).我确信这不是最优的(并且欢迎提出改进建议。)。

暂无
暂无

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

相关问题 有没有办法在没有 nuget package 的情况下使用 Microsoft.CodeDom.Providers.DotNetCompilerPlatform? - Is there a way to use Microsoft.CodeDom.Providers.DotNetCompilerPlatform without a nuget package? 使用“ Microsoft.CodeDom.Providers.DotNetCompilerPlatform”包时,csc.exe编译时间降低 - csc.exe compilation time degradation when “Microsoft.CodeDom.Providers.DotNetCompilerPlatform” package is used 无法加载文件或程序集“Microsoft.CodeDom.Providers.DotNetCompilerPlatform 或其依赖项之一。” 访问被拒绝 - Could not load file or assembly 'Microsoft.CodeDom.Providers.DotNetCompilerPlatform or one of its dependencies. Access is denied 无法找到CodeDom提供程序类型“Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider ...” - The CodeDom provider type “Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider …” could not be located "找不到 CodeDom 提供程序类型“Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider”" - The CodeDom provider type "Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider" could not be located IIS 7无法找到Microsoft.CodDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider DLL - IIS 7 unable to find Microsoft.CodDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider DLL 无法加载文件或程序集 Microsoft.Build.Utilities.Core。 为什么 DotNetCompilerPlatform 依赖于旧的 Build.Utilities.Core? - Could not load file or assembly Microsoft.Build.Utilities.Core. Why does the DotNetCompilerPlatform rely on an old Build.Utilities.Core? 卸载后是否出现Microsoft.AspNet.Providers错误? - Microsoft.AspNet.Providers errors after uninstalling? 无法找到 CodeDom 提供程序类型“Microsoft.VisualC.CppCodeProvider”,尝试编译 node_modules - CodeDom provider type 'Microsoft.VisualC.CppCodeProvider' could not be located, trying to compile node_modules Web 应用程序构建错误:无法找到 CodeDom 提供程序类型 Microsoft.VisualC.CppCodeProvider - Web Application Build Error: The CodeDom provider type Microsoft.VisualC.CppCodeProvider could not be located
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM