How to resolve Resharper's “unused property” warning on properties solely for Display/Value Members?

我只是一个虾纸丫 提交于 2019-12-12 09:33:06

问题


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 string 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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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).




回答5:


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;
    }
}



回答6:


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 if off project wide.



来源:https://stackoverflow.com/questions/3063844/how-to-resolve-resharpers-unused-property-warning-on-properties-solely-for-di

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!