问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
有unsafe关键字的C#代码片段,注意是字符串类型的,如
var str="unsafe struct Foo {}"
C#如何动态编译str变量中的代码。
有unsafe关键字的C#代码片段,注意是字符串类型的,如
var str="unsafe struct Foo {}"
C#如何动态编译str变量中的代码。
using System.CodeDom.Compiler;
using Microsoft.CSharp;
class Test
{
public static void Main(string[] args)
{
var compiler = new CSharpCodeProvider();
var parameters = new CompilerParameters {
CompilerOptions = "/unsafe"
};
var source = "unsafe struct Foo {}";
var result = compiler.CompileAssemblyFromSource(parameters, source);
// No errors are shown with the above options set
foreach (var error in result.Errors)
{
Console.WriteLine(error);
}
}
}
https://stackoverflow.com/questions/13392604/building-program-with-unsafe-code-in-codedom