我用一个列表包含从XML文件解析数据,使用字符串作为其成员:如何完全通过列表?
public class ServerList
{
public string ServerName { set; get; }
public string ServerReboot { set; get; }
public string ServerShutdown { set; get; }
public ServerList()
{
ServerName = "";
ServerReboot = "";
ServerShutdown = "";
}
}
从主窗体我启动的编辑形式和列表传递到它。在这个编辑器窗体上,用户可以添加或删除列表中的条目,也可以更改列表中的部分内容。如果他们点击确定按钮,我希望能够将编辑器表单中的列表拖回到主表单中,但是如果他们单击取消,我希望这些更改被删除。这是编辑的形式拉升方式:
private void mnuEdit_Click(object sender, EventArgs e)
{
frmEditor theEditor = new frmEditor();
theEditor.updatedServerList = theServerList;
DialogResult res = theEditor.ShowDialog();
if (res == DialogResult.OK)
{
theServerList = theEditor.updatedServerList.ToList();
SetupFilters(GroupOrBatch.Group);
// other processing to update the main form from the updated list
}
}
,然后在编辑表单上,这是它是如何获得:
public partial class frmEditor : Form
{
private List<ServerList> myServerList = new List<ServerList>();
public List<ServerList> updatedServerList
{
get { return myServerList; }
set { myServerList = value.ToList(); }
}
....
什么我发现是,虽然出现的列表结构是复制到新变量,实际数据仍然链接到原始列表。即使用户单击取消,并且修改后的列表不会被复制回原始,原件也已被更改。
这给我留下了两个选择之一 - 要么我可以找到某种方法来完成对新列表的全面深入克隆(可以在取消时删除),也可以完全删除取消按钮,所有修改都是现场的。
那么你的问题是什么?你知道你需要做什么,要么不允许取消或复制列表中的项目以及列表本身。那里没有问题。 – Servy
你的意思是你有一个类*,而不是一个列表 –
'要么我可以找到某种方法来完成一个新的列表克隆到一个新的列表(可以取消一个取消),或者我完全删除取消按钮' - 是的,这些是你的选择。或者让你的'ServerList'成为'struct',这样复制[自动完成](https://msdn.microsoft.com/en-us/library/aa664472%28v=vs.71%29.aspx) 。 – GSerg