在 .Net Core 中使用 CSharpCodeProvider

use*_*582 2 .net c# .net-core

我有一些 C# 代码从 SOAP WSDL 和 Swagger 文档动态生成源代码,然后使用 CSharpCodeProvider 类的名为 CompileAssemblyFromSource 的方法编译它。使用 .Net 4.8 可以正常工作,但是在尝试移植到 .Net Core 3.0 时,我现在收到此错误:

“此平台不支持操作。”

.Net Core 是否支持此功能,或者可能很快会添加?这是一个简化的代码摘录:

CSharpCodeProvider codeProvider = new CSharpCodeProvider();
string[] m_References = new string[] { "System.dll", "System.Core.dll", "System.Data.dll", "System.Net.dll", "System.Net.Http.dll", "System.Runtime.Serialization.dll", "System.ServiceModel.dll", "System.Xml.dll" };
CompilerParameters compilerParameters = CreateCompilerParameters(m_References);
m_CompilerResults = codeProvider.CompileAssemblyFromSource(compilerParameters, m_SourceStringArray);
if (!m_CompilerResults.Errors.HasErrors)
{
    /* render UI using compiled code */
}
else
{
    /* log compiler errors */
}
Run Code Online (Sandbox Code Playgroud)

Als*_*ein 5

它在 .NET Core 中已过时,您可以改用 roslyn ( github | nuget | docs )。