[英]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.