views:

36

answers:

3

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  A: 

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.

Adrian Grigore
I have discovered the suppress warning comments, however the example is fairly simplified; there are many such comboboxes with various objects, etc. I'd prefer not to have to add so many comments. Is there some other way to set Display and Value Members that don't rely on reflection?
JYelton
JYelton: Not that I'm aware of. As a last resort you could also suppress the warning globally.
Adrian Grigore
+1  A: 

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

BioBuckyBall
Is there some other way to set Display and Value Members that don't rely on reflection?
JYelton
Nope, I usually suppress with comment, then move the comment begin/close pair to suppress for the entire class.
BioBuckyBall
A: 

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.

David Williams
I suggest you show an example, including how to get the attributes. Some people don't get this.
John Saunders