C# 如何摆脱 Visual Studio 中的“[某些事件] 从未使用过”编译器警告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1093315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get rid of "[some event] never used" compiler warnings in Visual Studio?
提问by jedmao
For example, I get this compiler warning,
例如,我收到此编译器警告,
The event 'Company.SomeControl.SearchClick' is never used.
从不使用事件“Company.SomeControl.SearchClick”。
But I know that it's used because commenting it out throws me like 20 new warnings of XAML pages that are trying to use this event!
但我知道它被使用了,因为将它注释掉会让我像 20 个试图使用此事件的 XAML 页面的新警告一样!
What gives? Is there a trick to get rid of this warning?
是什么赋予了?有没有办法摆脱这个警告?
采纳答案by lc.
This appears to be warning 67and can thus be suppressed with:
这似乎是警告 67,因此可以通过以下方式进行抑制:
#pragma warning disable 67
Don't forget to restore it as soon as possible (after the event declaration) with:
不要忘记尽快(在事件声明之后)恢复它:
#pragma warning restore 67
However, I'd check again and make sure you're raisingthe event somewhere, not just subscribingto it. The fact that the compiler spits out 20 warningsand not 20 errorswhen you comment out the event is also suspicious...
但是,我会再次检查并确保您在某个地方提出该事件,而不仅仅是订阅它。当您注释掉事件时,编译器会吐出 20 个警告而不是 20 个错误这一事实也很可疑……
There's also an interesting articleabout this warning and specifically how it applies to interfaces; there's a good suggestion on how to deal with "unused" events. The important parts are:
还有一篇关于此警告的有趣文章,特别是它如何应用于接口;关于如何处理“未使用”事件有一个很好的建议。重要的部分是:
The right answer is to be explicit about what you expect from the event, which in this case, is nothing:
public event EventHandler Unimportant { add { } remove { } }
This will cleanly suppress the warning, as well as the extra compiler-generated implementation of a normal event. And as another added benefit, it prompts one to think about whether this do-nothing implementation is really the best implementation. For instance, if the event isn't so much unimportant as it is unsupported, such that clients that do rely on the functionality are likely to fail without it, it might be better to explicitly indicate the lack of support and fail fast by throwing an exception:
public event EventHandler Unsupported { add { throw new NotSupportedException(); } remove { } }
Of course, an interface that can be usefully implemented without some parts of its functionality is sometimes an indication that the interface is not optimally cohesive and should be split into separate interfaces.
正确的答案是明确您对事件的期望,在这种情况下,什么都不是:
public event EventHandler Unimportant { add { } remove { } }
这将完全抑制警告,以及正常事件的额外编译器生成实现。作为另一个额外的好处,它促使人们思考这种无所作为的实现是否真的是最好的实现。例如,如果事件不是不重要而是不受支持,以至于确实依赖该功能的客户端可能会在没有它的情况下失败,那么最好通过抛出例外:
public event EventHandler Unsupported { add { throw new NotSupportedException(); } remove { } }
当然,一个可以在没有其某些部分功能的情况下有效实现的接口有时表明该接口不是最佳内聚的,应该拆分为单独的接口。
回答by SLaks
The compiler is apparently not aware that it's being used in XAML code. Try suppressing the warning in your event definition.
编译器显然不知道它正在 XAML 代码中使用。尝试取消事件定义中的警告。
Also, make sure you're actually raising the event somewhere.
另外,请确保您实际上是在某处引发事件。
回答by Svend
You can supress individual warnings.
您可以抑制个别警告。
\Program.cs(13,20): warning CS0219: The variable 'foo' is assigned but its value is never used
In this case, CS0219 is the warning regarding variables being assigned but not used. You can either use the /nowarn:0219 flag, or add the error number in the properties pane for the project (under "Build", remember to remove the leading CS). Keep in mind the supresses allwarnings of this class.
在这种情况下,CS0219 是有关已分配但未使用的变量的警告。您可以使用 /nowarn:0219 标志,或在项目的属性窗格中添加错误编号(在“Build”下,记得删除前导 CS)。请记住,该类会抑制所有警告。
回答by Adam Mills
If you are forced to implement an event from an interface, that your implementation doesn't need you can do the following to avoid the warning.
如果您被迫从接口实现事件,您的实现不需要您可以执行以下操作以避免警告。
public event EventHandler CanExecuteChanged { add{} remove{} }
回答by Simon
Or you can add <NoWarn>67</NoWarn>
to your project
或者您可以添加<NoWarn>67</NoWarn>
到您的项目中
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
...
<NoWarn>67</NoWarn>
</PropertyGroup>
回答by vidstige
The second best way is imho to clearly state that the event is not supported by throwing an exception if someone tries to subscribe to it.
第二个最好的方法是恕我直言,如果有人试图订阅它,则通过抛出异常来明确说明该事件不受支持。
public event RoutedEventHandler SearchClick
{
add { throw new NotSupportedException(); }
remove { }
}
As a variant on this, you can also just leave the add
and remove
methods empty to silently ignore subscriptions on the event.
作为对此的一种变体,您还可以将add
和remove
方法留空以静默忽略事件的订阅。
The best solution is to refactor the code, perhaps pull the declaration of the event to the implementor if possible.
最好的解决方案是重构代码,如果可能的话,也许将事件的声明拉到实现者那里。
As a last resort, you can also disable the warning like so
作为最后的手段,您还可以像这样禁用警告
#pragma warning disable 67
public event RoutedEventHandler SearchClick;
#pragma warning restore 67
回答by GrantA
You can also do the following:
您还可以执行以下操作:
public event EventHandler MyEvent = delegate {}