在.netstandard 2.0上迁移后无法编译简单的动态代码(CodeDom抛出System.PlatformNotSupportedException) [英] Cannot compile simple dynamic code after migration on .netstandard 2.0 (CodeDom throws System.PlatformNotSupportedException)

查看:328
本文介绍了在.netstandard 2.0上迁移后无法编译简单的动态代码(CodeDom抛出System.PlatformNotSupportedException)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试编译此代码示例:

var c = new CSharpCodeProvider();
var cp = new CompilerParameters();
var className = $"CodeEvaler_{Guid.NewGuid().ToString("N")}";
// doesn't work with or without netstandard reference
var netstandard = Assembly.Load("netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51");
cp.ReferencedAssemblies.Add(netstandard.Location);
cp.CompilerOptions = "/t:library";
cp.GenerateInMemory = true;

var sb = new StringBuilder("");

sb.Append("namespace Jobs.Dynamic{ \n");
sb.Append($"public class {className} {{\n");
sb.Append($"public object RunSnippetCode()\n{{\n");
sb.Append("\nreturn null;\n");
sb.Append("\n}\n");
sb.Append("}");
sb.Append("}");

CompilerResults cr = c.CompileAssemblyFromSource(cp, sb.ToString());

在.netstandard 2.0上进行迁移之前,一切都很好

Everything was ok before migration on .netstandard 2.0

必须生成一个简单的类,并且只要复制代码并在Visual Studio中运行它,它就可以工作.具有一个返回null的方法的类.现在,抛出CompileAssemblyFromSource方法

A simple class has to be generated and it works if just copy the code and run it in Visual Studio. The class with one method that returns null. Now CompileAssemblyFromSource method throws

System.PlatformNotSupportedException:该平台不支持该操作.在Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch(CompilerParameters选项,String [] fileNames)在Microsoft.CSharp.CSharpCodeGenerator.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromSourceBatch(CompilerParameters选项,String []源)在CnetContent.Jobs.DynamicCode..ctor(IEnumerable 1 referencedAssemblies,IEnumerable 1 usings,String methodArgs,String codeSnippet)在CnetContent.Jobs.Core.JobBase.EvalRunCondition(String& error)

System.PlatformNotSupportedException: Operation is not supported on this platform. at Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch(CompilerParameters options, String[] fileNames) at Microsoft.CSharp.CSharpCodeGenerator.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromSourceBatch(CompilerParameters options, String[] sources) at CnetContent.Jobs.DynamicCode..ctor(IEnumerable1 referencedAssemblies, IEnumerable1 usings, String methodArgs, String codeSnippet) at CnetContent.Jobs.Core.JobBase.EvalRunCondition(String& error)

我更新了System.CodeDom,并且该库支持.netstandard 2.0,但是此代码仍然无法正常工作.找不到任何类似问题的案例.有任何想法吗?预先谢谢你!

I updated System.CodeDom and this library supports .netstandard 2.0, but this code still doesn't work. Could not find any cases with similar problems. Any ideas? Thank you in advance!

推荐答案

.NetCore 2存在相同的问题,但是CodeDOM项目更大.我现在正在构建的解决方案是从CodeDom生成源,然后将其传递给Roslyn.(正如Roslyn在评论中提到的那样,但仅发布了.NET Framwork解决方案)

I had the same problem with .NetCore 2, but with a bigger CodeDOM project. My solution I am right now building is generating the source from CodeDom and then passing it to Roslyn. (As Roslyn was mentioned in a comment, but only a .NET Framwork solution was posted)

这是一个如何使用Roslyn的好例子-只需添加

Here is a good example how to use Roslyn - just add the

Microsoft.CodeAnalysis.CSharp NuGet程序包和 System.Runtime.Loader NuGet包

Microsoft.CodeAnalysis.CSharp NuGet package and System.Runtime.Loader NuGet package

,然后在此处使用代码(或仅遵循示例): https://github.com/joelmartinez/dotnet-core-roslyn-sample/blob/master/Program.cs

and then use the code here (Or just follow the example): https://github.com/joelmartinez/dotnet-core-roslyn-sample/blob/master/Program.cs

这篇关于在.netstandard 2.0上迁移后无法编译简单的动态代码(CodeDom抛出System.PlatformNotSupportedException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆