简体   繁体   English

CsScript with Mono:如何使Mono不将警告视为错误

[英]CsScript with Mono: How to make mono not treat warnings as errors

I use CSScriptLibrary.dll to execute C# code in my application which runs on both Windows and Linux. 我使用CSScriptLibrary.dll在Windows和Linux上运行的应用程序中执行C#代码。 Problem is, right now, I need to use #pragma disable warning to disable all kinds of warnings that may arise in order to get the scripts to compile on Mono which is a very ugly hack. 问题是,现在,我需要使用#pragma disable warning禁用可能出现的各种警告,以使脚本可以在Mono上编译,这是一个非常丑陋的技巧。

// the following simple script will not execute on Mono due to a warning that a is not used.
var code = "public class Script { public object Run() { var a=1; return 2+3; }}"
// here is how the script is executed using CsScriptLibrary
try
{
    var asm = new AsmHelper(CSScript.LoadCode(code, "cs", null, true));
    // if we reach that point, the script compiled
    var obj = asm.CreateAndAlignToInterface<IScript>("*");
    // now run it:
    var result=obj.Run();
}
catch (CompilerException e)
{
    // on .net compiler exceptions are only raised when there are errors
    // on mono I get an exception here, even for warnings like unused variable
}    

I already tried to set the default compiler parameters of CSScript to instruct the mono compiler to disregard warnings. 我已经尝试设置CSScript的默认编译器参数,以指示Mono编译器忽略警告。 Here is what I tried (based on documentation of compiler switches of Mono compiler: 这是我尝试过的(基于Mono编译器的编译器开关文档:

CSScript.GlobalSettings.DefaultArguments = "-warn:0 -warnaserror-";

But I had no success and I am not even sure if this is the right way to go. 但是我没有成功,我什至不确定这是否是正确的方法。 Anyway, for the sake of completeness I note here that CSScript.GlobalSettings.DefaultArguments defaults to /c /sconfig /co:/warn:0 in CSScript. 无论如何,为了完整起见,我在这里注意到CSScript.GlobalSettings.DefaultArguments默认为/c /sconfig /co:/warn:0

Does anyone know how to get CSScript.LoadCode to disregard warnings on Mono or at least not treat them as errors? 有谁知道如何获取CSScript.LoadCode忽略Mono上的警告,或者至少不将其视为错误?

Here are two solutions (found with help of Oleg Shilo). 这是两个解决方案(在Oleg Shilo的帮助下找到)。 You can either include the desired compiler option directly in the script: 您可以直接在脚本中包含所需的编译器选项:

//css_co -warn:0 
using System;
...

or you can substitute CSScript.LoadCode with LoadWithConfig , which allows passing compiler options directly. 或者,您可以将CSScript.LoadCode替换为LoadWithConfig ,从而允许直接传递编译器选项。 Something like this: 像这样:

static public Assembly LoadCode(string scriptText, bool debugBuild, params string[] refAssemblies)
{
    string tempFile =  System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() +".cs";        
    try
    {
        using (StreamWriter sw = new StreamWriter(tempFile))
            sw.Write(scriptText);        
        return LoadWithConfig(scriptFile, null, debugBuild, CSScript.GlobalSettings, "-warn:0", refAssemblies);
    }
    finally
    {
        if (!debugBuild)
        {
            //delete temp file
        }
    }
}

It should be noted, that the second solution will bypass the built in assembly caching performed in LoadCode. 应当注意,第二种解决方案将绕过在LoadCode中执行的内置程序集缓存。 It is easy enough to cache the compiled script object though. 不过,缓存已编译的脚本对象非常容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM