我有一段代码,Resharper 告诉我有未使用的变量,但这些变量肯定是使用的。变量在 Databind() 中使用,要绑定的字段被指定为字符串。由于使用字符串变量访问字段名称,Resharper 认为它们没有被使用。
在下面的代码示例中,Resharper 告诉我将公共变量更改为私有。这样做之后,它告诉我该变量未使用并且可以删除。这两个建议都是错误的,因为使用了变量并且必须是公共的。
我不喜欢 Resharper 警告我这个并且是黄色的。我想检查我的绿色代码。我知道我可以使用取消注释的选项来忽略这一点,但过去我从来没有使用过这个选项,并且能够找到其他解决方案来让我的代码变绿。在这种情况下,我无法找到另一种方法。有谁知道我怎样才能让 Resharper 识别出这个变量正在被使用?
using System;
using System.Collections;
using System.Web.UI.WebControls;
public partial class TestCode_General_ResharperTest : System.Web.UI.Page
{
private class TestClass
{
public TestClass(string name, string id)
{
ID = id;
Name = name;
}
public string ID; /*Resharper says this can be made private*/
public string Name; /*Resharper says this can be made private*/
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropDownList testList = new DropDownList();
ArrayList groups = getTestList();
testList.DataSource = groups;
testList.DataValueField = "ID";
testList.DataTextField = "Name";
testList.DataBind(); /* Databind causes the public variables to be accessed.*/
}
}
private static ArrayList getTestList()
{
ArrayList groupInfo = new ArrayList();
string[] pairs = new[] { "Test:1", "Test 2:2", "Test 3:3" };
foreach (string pair in pairs)
{
string[] values = pair.Split(new[] { ':' });
groupInfo.Add(new TestClass(values[0], values[1]));
}
return groupInfo;
}
}