【发布时间】:2019-09-30 06:20:12
【问题描述】:
我想为我的类 TreeNode 制作一个深层副本。这是我的代码:
public TreeNode(TreeNode node, GUIStyle inPointStyle, GUIStyle outPointStyle, Action<ConnectionPoint> OnClickInPoint, Action<ConnectionPoint> OnClickOutPoint)
{
this.rect = new Rect(node.rect);
this.style = new GUIStyle(node.style);
this.inPoint = new ConnectionPoint(this, ConnectionPointType.In, inPointStyle, OnClickInPoint);
this.outPoint = new ConnectionPoint(this, ConnectionPointType.Out, outPointStyle, OnClickOutPoint);
this.defaultNodeStyle = new GUIStyle(node.defaultNodeStyle);
this.selectedNodeStyle = new GUIStyle(node.selectedNodeStyle);
this.allDecorations = new List<GameObject>(node.allDecorations);
this.objs = new Dictionary<GameObject, IndividualSettings>(node.objs);
this.name = String.Copy(node.name);
this.RemoveClonedObj = new Action(node.RemoveClonedObj);
this.OnChangeView = new Action<TreeNode>(node.OnChangeView);
this.OnRemoveNode = new Action<TreeNode>(node.OnRemoveNode);
this.OnCopyNode = new Action<TreeNode>(node.OnCopyNode);
this.PreviewTree = new Action<TreeNode, bool> (node.PreviewTree);
}
但是,骑士给了我警告:
Rider 似乎在说我的“新”毫无意义。
如果我按照 Rider 的指示,使用this.RemoveClonedObj = node.RemoveClonedObj; 删除原始 TreeNode 后,我复制的 TreeNode 的操作会发生什么?它们也会被删除吗?如果是这样,为什么 Rider 会给我这样的警告?
【问题讨论】:
-
你能不能也给我们看一下
TreeNode这个类? -
似乎
node.PreviewTree已经属于Action<TreeNode,bool>类型,因此从现有的操作创建新操作是多余的。在大多数情况下,this也是多余的,除非您使用的参数或局部变量与类字段的名称完全相同。只需使用PreviewTree = node.PreviewTree;