0
我有类Attribut和值。我不知道如何克隆列表中的项目值。C#如何克隆列表列表
public class Attribut : ICloneable
{
public Attribut()
{ }
public Attribut(List<Values> value)
{
this.Values = value;
}
public object Clone()
{
return this.MemberwiseClone();
}
//other members
List<Values> values = new List<Values>();y
public List<Values> Values
{
get { return values; }
set { values = value; }
}
}
[Serializable]
public class Values : ICloneable
{
public Values()
{
}
public Values(string name)
{
this.Name = name;
}
public Values(string name, int count)
{
this.Name = name;
this.Count = count;
}
public object Clone()
{
return this.MemberwiseClone();
}
string name;
int count = 0;
public int Count
{
get { return count; }
set { count = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
}
我有问题克隆值时,我把这种方法(attrList)是全球性的变量) 当我添加新的值到列表中,相同的值也被添加在attrList。
private List<Attribut> getSelectedColumns()
{
List<Attribut> list = new List<Attribut>();
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].Checked == true)
{
list.Add((Attribut)attrList[i].Clone());
for(int j= 0; j < attrList[i].Values.Count;j++)
{
list[j].Values.Add((Values)attrList[i].Values[j].Clone());
}
}
}
你的克隆方法是如何定义的? – 2013-04-10 21:34:35
你到底在做什么?我无法想象它应该是这么难。 – 2013-04-10 21:42:39
方法在类外观代码中定义。我有插件应用程序,我需要发送metada插件。在attrList都是元数据,并在列表被选中metadata.And我在插件 –
Sejnt
2013-04-10 23:28:41