A.R*_*R.F 5 c# .net-core asp.net-core-2.0
我有一个简单的项目.NetFramework 3.5 Class library。
我在项目中的(with )c# dynamic code处编译并运行,并从项目中调用。runtimeCodeDom.NetFrameworkAsp.NetCore 2.0
当我从项目中调用类BuildAssembly中的方法时,遇到此错误:(CodeDom 抛出)CommonAsp.NetCore 2.0
"System.PlatformNotSupportedException":"此平台不支持该操作。" 在 Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch(CompilerParameters 选项,String[] fileNames) 在 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
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");
}
Run Code Online (Sandbox Code Playgroud)
我在网上搜索并按照此链接安装Microsoft.CodeAnalysis.CSharpnuget 包并进行了测试,但它不起作用。
我无法通过这种方式编译简单的代码。
如何修复这个错误?
| 归档时间: |
|
| 查看次数: |
8662 次 |
| 最近记录: |