C# - 禁用CS0444警告

时间:2011-03-23 07:59:16

标签: c# warnings

我发出此警告是因为我创建了类型:

#if COMPACT_FRAMEWORK_3_5

namespace System.Diagnostics
{
    /// <summary>Determines how a class or field is displayed in the debugger variable windows.</summary>
    /// <remarks>See : http://msdn.microsoft.com/en-us/library/system.diagnostics.debuggerdisplayattribute.aspx </remarks>
    [AttributeUsage(AttributeTargets.Delegate | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Enum | AttributeTargets.Struct | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true)]
    [ComVisible(true)]
    public sealed class DebuggerDisplayAttribute : Attribute
    { /* ... */ }
}
#endif

我已创建它因为我在CF.NET上并且该属性不存在,因此我创建它以便自定义我的调试视图(我总是使用CF.NET进行调试)。

我试图在开头添加#pragma warning disable并在结束时恢复它但不起作用,我在VS2k8中总是发出CS0444警告......

我该如何解决这个问题?

由于


修改

我这样用过:

#if COMPACT_FRAMEWORK_3_5
#pragma warning disable // 0444

namespace System.Diagnostics
{
    /// <summary>Determines how a class or field is displayed in the debugger variable windows.</summary>
    /// <remarks>See : http://msdn.microsoft.com/en-us/library/system.diagnostics.debuggerdisplayattribute.aspx </remarks>
    [AttributeUsage(AttributeTargets.Delegate | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Enum | AttributeTargets.Struct | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true)]
    [ComVisible(true)]
    public sealed class DebuggerDisplayAttribute : Attribute
    { /* ... */ }
}
#pragma warning restore // 0444
#endif

2 个答案:

答案 0 :(得分:3)

你的例子中注明了数字:

#pragma warning disable // 0444

呃,不要:

#pragma warning disable 444

要全局禁用它,请使用编译器使用/nowarn:444(逗号分隔其他警告来禁用)。如果使用IDE,可能会在项目属性中的某个位置设置警告以禁用(Visual Studio在“Build”选项卡上有它,SharpDevelop在“编译”选项卡上有它,其他IDE可能会在某处类似) 。 Nant脚本将<onwarn>元素作为<csc>的允许子元素(也具有nowarn属性,但已弃用)。

我会避免全局禁用,毕竟,做一些触发CS0444的事情通常是个坏主意。在你的情况下,你有充分的理由这样做(你需要紧凑版本没有给你的功能)并知道你在做什么,但如果你不小心在其他地方做了,你就会被压制警告,你不会更聪明。

编辑:就此而言,我有一个政策,我从不禁用警告,至少没有简短的评论为什么。一个未注释的警告禁用可能意味着“我在这里做了一些愚蠢的事情,然后禁用警告而不是修复它”,评论可以清楚地说明情况并非如此,并且没有任何需要修复的事情。

答案 1 :(得分:1)

嗯,也许试试

#pragma warning disable 444

而不是评论错误号码?当然可以参见here