我是新手,所以如果这是一个愚蠢的问题,我深表歉意。我已将我的接口绑定到我构建的类的字段。我将类作为 T 类型的列表启动,现在我试图将字段分配增加到列表的索引。
我的代码看起来像这样。我的课:
class MyClass
{
public string ID { get; set; } // I want these to increment
public string MyVar { get; set; }
public MyClass()
{
this.ID = "";
this.MyVar = "";
}
public MyClass (string id, string myvar)
{
this.ID = id;
this.MyVar = myvar
}
public static List<MyClass> Query(string datasource)
{
XElement data = XElement.Load(datasource);
List<MyQuiz> query = (from d in data.Descendants("Object")
select new MyQuiz((string)d.Element("ID"),
(string)d.Element("MyVar")
)).ToList();
return query;
}
public static List<MyClass> Assign (List<MyClass> assign, int i)
{
i++
//Can't do this because it's static, need another way!
this.ID = assign[i].ID;
this.MyVar = assign[i].MyVar;
}
启动代码
int i = 0;
List<MyClass> myClass = new List<MyClass>();
myClass = MyClass.Query("data.xml");
MyClass.Assign(myClass, i);
和xml
<root>
<Object>
<ID>1</ID>
<MyVar>TESTING</MyVar>
</Object>
<root>
除了 Assign 方法外,一切正常,有什么建议吗?