该平台不支持该操作

我有一个.NetFramework 3.5 Class library的简单项目。

我在c# dynamic code项目的runtime(使用CodeDom)上编译并运行.NetFramework,并从Asp.NetCore 2.0项目进行调用。

当我从BuildAssembly项目的Common类中调用Asp.NetCore 2.0方法时,遇到此错误:(CodeDom引发)

  

“ System.PlatformNotSupportedException”:“不支持该操作   该平台。”位于microsoft.CSharp.CSharpCodeGenerator.FromFileBatch(CompilerParameters选项,String []文件名),位于microsoft.CSharp.CSharpCodeGenerator.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromSourceBatch(CompilerParameters选项,String []源)

以下代码:

.NetFrameWork 3.5:

public class Common
{
    public Assembly BuildAssembly(string code)
    {
        CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
        CompilerParameters compilerparams = new CompilerParameters();
        compilerparams.GenerateExecutable = false;//Generate an executable instead of a class library
        compilerparams.GenerateInmemory = false; //Save the assembly as a physical file.

        compilerparams.ReferencedAssemblies.Add("mscorlib.dll");
        compilerparams.ReferencedAssemblies.Add("System.dll");
        compilerparams.ReferencedAssemblies.Add("System.Data.dll");
        compilerparams.ReferencedAssemblies.Add("system.xml.dll");

        CompilerResults results = codeProvider.CompileAssemblyFromSource(compilerparams,code);
        if (results.Errors.HasErrors)
        {
            StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
            foreach (CompilerError error in results.Errors)
            {
                errors.AppendFormat("Line {0},{1}\t: {2}\n",error.Line,error.Column,error.ErrorText);
            }
            throw new Exception(errors.ToString());
        }
        else
        {
            return results.CompiledAssembly;
        }
    }
}

Asp.NetCore 2.0:

public IactionResult Test()
{
    string code = @"
using System;
using System.Text;
using System.Data;
using System.Reflection;
using System.ComponentModel;

namespace testNameSpace
{
    public class DynamicClass
    {
         public string Test() {     return ""Hello world""; }
    }
}";
    var res = new Common().BuildAssembly(code);
    return Json("ok");
}

我在网上搜索并编码了this,安装了microsoft.CodeAnalysis.CSharp nuget程序包并进行了测试,但无法正常工作。

我无法通过这种方式来编译简单的代码。

如何解决此错误?

looven23 回答:该平台不支持该操作

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3119549.html

大家都在问