How to resolve ReSharper's "unused property" warning on properties solely for Display/Value Members?
I have defined two properties, "Name" and "ID", for an object which I use for the DisplayMember and ValueMember of a ComboBox with a BindingList datasource.
I recently installed ReSharper to evaluate it. ReSharper is giving me warnings on the object that the two properties are unused.
Sample code:
BindingList<ClassSample> SampleList = new BindingList<ClassSample>();
// populate SampleList
cmbSampleSelector.DisplayMember = "Name";
cmdSampleSelector.ValueMember = "ID";
cmbSampleSelector.DataSource = SampleList;
private class ClassSample
{
private string _name;
private string _id;
public stri开发者_如何学运维ng Name // ReSharper believes this property is unused
{
get { return _name; }
}
public string ID // ReSharper believes this property is unused
{
get {return _id; }
}
public ClassSample(string Name, string ID)
{
_name = Name;
_id = ID;
}
}
Am I doing something wrong or is ReSharper clueless about this particular usage?
The way that JetBrains suggests that you solve these issues is with their attributes (available from ReSharper -> Options -> Code Annotations). Add the attributes to your project/solution and then mark these properties with the UsedImplicitly attribute. ReSharper will now assume that the properties are used via Reflection or late bound or whatever.
Just to add to David's answer, in Resharper 8.x and later, add the Resharper.ExternalAnnotations
plugin to Resharper in Visual Studio (Resharper -> Extension Manager)
.
When Resharper next complains about an unused property
, you can then click on the left hand purple pyramid and select Used Implicitly
, which will decorate the field / property with an UsedImplicitlyAttribute
.
You can then either directly add a reference to JetBrains.Annotations.dll
in your project, or you can choose to let Resharper add a small Annotations.cs
file to your project containing the Attribute
definition (and others like NotNullAttribute
). The Annotations.cs
file is located under the Properties
icon in the solution folder.
As an aside, it would have been nice to be to be able to add a Description
to the attribute as well, e.g. [UsedImplicitly(Description="Used by NUnit Theory / Reflected by Xyz, etc")]
, so in the interim we'll need to comment.
You are setting the properties via reflection (this is what the code above amounts to). ReSharper can only analyze static behavior (with some very limited exceptions), so it can't know that these properties are actually being used.
You can simply suppress the warnings though. Just click on the property name and hit Alt+Enter, then choose the option to suppress the warning. This will add a // ReSharper disable
comment around the variable name.
When things are used via reflection (which I imagine is what the framework is doing in your case), ReSharper can't tell (at least in it's current form).
I did use the ReSharper mechanism for commenting out that check for a specific instance which I also needed, using this process with ReSharper:
This worked great for what I needed in this where I am using an implicit mechanism for setting the value but do not want to turn it off project wide.
To install ReSharper annotations:
PM> Install-Package JetBrains.Annotations
Then you can add the attributes by typing [UsedImplicitly]
or by using the context menu (screenshot).
Code sample:
using JetBrains.Annotations;
class ClassSample
{
private string _name;
private string _id;
[UsedImplicitly]
public string Name // ReSharper believes this property is unused
{
get { return _name; }
}
[UsedImplicitly]
public string ID // ReSharper believes this property is unused
{
get { return _id; }
}
public ClassSample(string Name, string ID)
{
_name = Name;
_id = ID;
}
}
精彩评论