如何限制将项目添加到List <t>?</t>

时间:2009-10-07 11:43:26

标签: c# list properties code-access-security

我有名为Person的类,包含属性,父亲和儿童名单。

我希望每个人只使用AddChild方法添加子项,而不是List.Add方法, 那么如何限制它的使用?

public class Person
{
  private List<Person> _children = new List<Person>();
  public string Name { get; set; }
  public Person Father { get; set; }
  public List<Person> Children 
  { 
    get
    {
       return _children;
    } 
  }
  public void AddChild(string name)
  {
      _children.Add( new Person { Name = name, Father = this });
  }
}

7 个答案:

答案 0 :(得分:12)

将子项公开为ReadOnlyCollection:

public IList<Person> Children  
{  
    get 
    { 
       return new ReadOnlyCollection<Person>(_children);
    }  
} 

答案 1 :(得分:8)

Children公开为IEnumerable<T>

答案 2 :(得分:8)

将您的儿童属性更改为:

public IList<Person> Children 
{ 
  get
  {
     return _children.AsReadOnly();
  } 
}

答案 3 :(得分:7)

如果你暴露了潜在的List<T>,那么简而言之:你不能。

您可以编写自己的集合包装类,也可以继承Collection<T>(仍然会公开Add,但您可以override一些事情来检查数据添加)。

答案 4 :(得分:3)

将Children属性公开为ReadOnlyCollection<Person>

public ReadOnlyCollection<Person> Children
{
     get {return _children.AsReadOnly();}
}

答案 5 :(得分:3)

如果您使用的是.NET 4.5或更高版本,则可以将_children作为IReadOnlyList<T>返回:

public IReadOnlyList<Person> Children
{
    get
    {
        return _children;
    }
}

与通过IList<Person>返回_children.AsReadOnly()有什么不同? IReadOnlyList<Person>甚至没有变异方法。请考虑以下代码:

somePerson.Children[0] = null;

使用IReadOnlyList<Person>时,此代码将无法编译。使用.AsReadOnly()时,此代码将导致运行时异常。

与通过ReadOnlyCollection<Person>返回_children.AsReadOnly()有什么不同?没有创建ReadOnlyCollection<Person>包装器对象。除此之外,我没有看到巨大的差异。

答案 6 :(得分:2)

IEnumerable工作得很好:

public IEnumerable<Person> Children
{
    get
    {
        return _children.AsReadOnly();
    }
}

或更长的啰嗦:

public IEnumerable<Person> Children
{
    get
    {
        foreach (Person child in _children)
        {
            yield return child;
        }
    }
}
相关问题