Skip to main content
 首页 » 编程设计

C#之禁用 CS0444 警告

2024年02月23日20birdshome

我有这个警告是因为我创建了类型:

#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 上并且它不存在这个属性,所以我创建它是为了自定义我的调试 View (我总是使用 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 

请您参考如下方法:

您的示例中的数字被注释掉了:

#pragma warning disable // 0444 

呃,不要:

#pragma warning disable 444 

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

不过,我会避免全局禁用,毕竟,执行触发 CS0444 的操作通常不是一个好主意。在你上面的例子中,你有一个很好的理由这样做(你需要紧凑版本没有给你的功能)并且知道你在做什么,但是如果你不小心在其他地方做了它,你就会被压制警告,您将一无所知。

编辑:就此而言,我制定了一项政策,即在没有至少简短说明原因的情况下,我永远不会禁用警告。未注释的禁用警告可能意味着“我在这里做了一些愚蠢的事情,然后禁用了警告而不是修复它”,注释可以清楚地表明情况并非如此,并且没有什么需要修复的。