12

I use CSharpCodeProvider to compile instant plugins for my app.

Right now it is possible to try to compile a file, that looks good, but generates many errors, for example a C# code glued with a binary file. There are many characters, that are treated with error CS1056: Unexpected character.

This behaviour is expected, but a compilation process of such a "malicious" file is very time consuming.

One solution that I find reasonable would be to limit the number of errors, after which CSharpCodeProvider.CompileAssemblyFromFile returns.

Is it possible to set such a limit?

I don't really want to inspect the file very carefully in the first place, if it's possible to avoid.

4

1 回答 1

1

我不知道是否有 CSharpCodeProvider 的属性或字段可以控制这一点,但一个可能的解决方案是更改您要问的问题 - 如果问题是尝试编译所需的时间,而不是错误的数量,我的解决方案是在单独的任务上运行 CSharpCodeProvider,但如果完成时间过长,则终止该任务。

然后你可以提供你自己的错误信息:

“你的代码太烂了,5分钟以内编译不出来。” :P

至于安全性,您可以尝试通过 Application.Run 在单独的 AppDomain 中运行东西吗?不知道您的应用程序有多复杂,但您可能会使用注入的插件启动主窗体的新实例。

于 2014-02-10T04:18:22.817 回答