我正在使用 Microsoft.CSharp.CSharpCodeProvider 以编程方式编译程序集并从外部 .cs 文件运行一些函数。
但是有没有办法禁止这些功能对文件系统等进行任何更改(我需要在某种沙箱中运行它们)?
我正在考虑向 CompilerParameters 添加“-nostdlib”选项,这样生成的程序集将无法访问 System.IO.File 和其他类。但我仍然需要 System.Object、System.ValueType 等。
现在我收到这些错误:
error CS0518: The predefined type `System.Object' is not defined or imported
error CS0518: The predefined type `System.ValueType' is not defined or imported
error CS0518: The predefined type `System.Attribute' is not defined or imported
error CS0518: The predefined type `System.Int32' is not defined or imported
... (too many of them)
如何添加那些没有可能严重损害系统的类(如 System.IO.File、System.IO.Directory、System.Net.(something)、System.Threading.(something)?)
或者,也许有更简单的方法?谢谢你。