两个TreeNodeCollection,将其中一个的节点赋给另一个:
TreeNodeCollection a = new TreeNodeCollection(); TreeNodeCollection b = this.treeview.nodes; foreach (TreeNode tn in b) { a.Add(tn); }
调试的时候发觉,在执行了a.Add(tn)之后,发觉b中的该节点没了,原以为节点最多指向同样的内存地址,
却没有想到竟然没了,查看相关源代码之后才发觉。。。原来Add方法会将其移除掉:
public void AddAt(int index, TreeNode child)
{
if (child == null)
{
throw new ArgumentNullException("child");
}
if (this._updateParent)
{
if ((child.Owner != null) && (child.Parent == null))
{
child.Owner.Nodes.Remove(child);
}
if (child.Parent != null)
{
child.Parent.ChildNodes.Remove(child);
}
if (this._owner != null)
{
child.SetParent(this._owner);
child.SetOwner(this._owner.Owner);
}
}
this._list.Insert(index, child);
this._version++;
if (this._isTrackingViewState)
{
((IStateManager) child).TrackViewState();
child.SetDirty();
}
this.Log.Add(new LogItem(LogItemType.Insert, index, this._isTrackingViewState));
}