Prevention of warnings "Possible exclusion of refusal link"

Let's say I have a read-only interface and a specific class in which the property is created in the constructor and marked as read-only.

internal interface IExample { ObservableCollection<string> Items { get; } } internal class Example : IExample { private readonly ObservableCollection<string> _items; public Example() { _items = new ObservableCollection<string>(); } public ObservableCollection<string> Items { get { return _items; } } } 

When I use the interface, Resharper warns me that I may have a possible null reference when calling the code.

 public class ExampleWithWarnings { public void Show() { IExample example = new Example(); // resharper warns about null reference example.Items.Add( "test" ); } } 

I understand that by definition, an interface does not guarantee that a property will matter. (I also admit that the properties on the interfaces are not perfect). But I know that this property will always matter.

Is there any magic attribute that I can set on an interface that prevents Resharper from showing a warning? I would prefer not to decorate all the class customs with a ban warning.

+6
source share
2 answers

Yes, there is an attribute that you can use: JetBrains.Annotations.NotNullAttribute . But you do not need to add a link to ReSharper in your project. You can use your own implementation: open the ReSharper options, and in the section "Code Verification"> "Code Annotations" you will find "Copy the standard implementation to the clipboard". Now just paste this into the code file in your project. You can even change the namespace.

The code annotations settings

And then remove the attribute in the interface property.

You should also look under "Code Verification"> "Settings" and select "Assume that the object may be empty ... when the object is explicitly marked with the CanBeNull attribute or is marked with zero." This way, you only receive warnings in members that you explicitly flag as unpleasant.

The Code Inspection settings

+7
source share

You can reduce this warning to a sentence. You can also edit external annotation files to create custom rules or behavior: http://msmvps.com/blogs/peterritchie/archive/2008/07/21/working-with-resharper-s-external-annotation-xml-files. aspx

+3
source share

All Articles