正确的方法来设置在C#中的树节点的家长吗? [英] Proper way to set parent of a tree node in C#?

查看:138
本文介绍了正确的方法来设置在C#中的树节点的家长吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个自定义的树。每个节点(BindingNode是我怎么称呼他们)包含实现的ICollection 并且包含更多的节点。我的问题是,用户应该能够访问每个节点的父与母称为一个只读属性,就像在的树节点。但是,节点通过调用BindingNodeCollection的添加(BindingNode节点)方法添加到BindingNodeCollection。 BindingNodeCollection知道它自己的父母是什么,但是,不能在添加(BindingNode节点)方法分配BindingNode的家长,因为家长应该是只读的。我想实现的功能基本上是一样的的树节点 TreeNodeCollection 的TreeView

I'm working on a custom tree. Each node (BindingNode is how I call them) contains a BindingNodeCollection that implements ICollection and contains more nodes. My problem is that, the user should be able to access each node's parent with a readonly property called Parent, just like in a TreeNode. But the nodes are added to a BindingNodeCollection by calling BindingNodeCollection's Add(BindingNode node) method. BindingNodeCollection knows what it's own parent is, but, can't assign BindingNode's Parent in the Add(BindingNode node) method because Parent should be readonly. The functionality I'm trying to achieve is basically the same as the functionality of a TreeNode and a TreeNodeCollection in a TreeView:

-TreeNode的父母是只读的。

-TreeNode's Parent is readonly.

-TreeNodes通过调用分配给TreeNodeCollection TreeNodeCollection的添加(TreeNode的节点)方法。

-TreeNodes are assigned to a TreeNodeCollection by calling TreeNodeCollection's Add(TreeNode node) method.

-TreeNode的家长给出了包含在添加树节点的TreeNodeCollection树节点。

-TreeNode's Parent gives the TreeNode that contains the TreeNodeCollection where the TreeNode was added.

-TreeNodeCollection不是内部树节点,以便能够访问父私有字段定义。

-TreeNodeCollection is NOT defined inside TreeNode in order to have access to the Parent private field.

这是当我想念C ++的朋友。 (

This is when I miss C++'s friend. :(

推荐答案

有几种方法,你可以实现你后,这真的取决于有多少安全你设想需要。这是一个图书馆或'封闭'的应用程序,只有你或你的同事会触动?这里有一些建议和问题之前,他们去考虑。

There are several methods that you can achieve what you're after, it really depends on how much security you envision needing. Is this a library or a 'closed' application that only you or your colleagues will touch? Here are some suggestions and questions to consider before going with them.


  1. 请在BindingNode属性setter是内部设置;?
    您是否信任大家的是,在本次大会不孤儿

揭露BindingNodeCollection为只读,并提供了一些方法来修改集合
上的BindingNode本身。在添加可设置专用Parent属性。删除将被要求设置父财产回空。的这是相当锁定,被污染的基础节点类值得安心?

Expose the BindingNodeCollection as ReadOnly and provide methods to modify the collection on the BindingNode itself. The Add can set the private Parent property. Remove will be required to set the Parent property back to null. This is pretty locked down, is polluting your base node class worth the peace of mind?

接受的透明度比安全性对你更重要,并揭露父二传手公共你信任的每个人,可以看到你的类不是孤儿?

Accept that transparency is more important than security for you, and expose the Parent setter as public. Do you trust everyone that can see your classes not to orphan children?

有关进一步的阅读,也许更好的想法,有什么你几乎描述是的组合模式。有一个 TON的安全性与透明度,这种模式的讨论并大部分涉及揭露这些类为市民在一些API来消费。这可能是矫枉过正您的情况,也可能是你需要什么考虑,我也没办法给你的问题的范围是什么。

For further reading and maybe better ideas, what you're almost describing is the Composite Pattern. There is a TON of discussion on security versus transparency for this pattern and most of it relates to exposing these classes for the public to consume in some API. It may be overkill for your situation, or it may be exactly what you need to consider, I have no idea given your question what the scope is.

这篇关于正确的方法来设置在C#中的树节点的家长吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆