3

一般来说,我正在制作的程序涉及存储少量可以分类的条目(在任何给定时间可能少于 30 个)。我希望允许看到这些条目,但不要在课堂外使用它们进行更改。我创建了一个可以修改的名为 Entry 的类和另一个名为 ReadOnlyEntry 的类,它是一个 Entry 对象的包装器。组织这些 Entry 对象的最简单方法似乎是创建一个List<List<Entry>>,其中每个List<Entry>都是一个类别。但随后以只读方式公开这些数据变得混乱和复杂。我意识到我必须拥有以下每种类型的一个对象:

List<List<Entry>> data;
List<List<ReadOnlyEntry>>  //  Where each ReadOnlyEntry is a wrapper for the Entry in the same list and at the same index as its Entry object.
List<IReadOnlyCollection<ReadOnlyEntry>>  //  Where each IReadOnlyCollection is a wrapper for the List<ReadOnlyEntry> at the same index in data.
IReadOnlyCollection<IReadOnlyCollection<ReadOnlyList>> readOnlyList  //  Which is a wrapper for the first item I listed.

列表中的最后一项将公开。第一个让我更改条目,第二个让我添加或删除条目,第三个让我添加或删除类别。每当数据发生变化时,我都必须保持这些包装器的准确性。这对我来说似乎很复杂,所以我想知道是否有更好的方法来处理这个问题。

编辑 1:澄清一下,我知道如何使用 List.asReadOnly(),我上面建议做的事情将解决我的问题。我只是有兴趣听到更好的解决方案。让我给你一些代码。

class Database
{
    //  Everything I described above takes place here.

    //  The data will be readable by this property:
    public IReadOnlyCollection<IReadOnlyCollection<ReadOnlyList>> Data
    {
        get
        {
            return readOnlyList;
        }
    }

    //  These methods will be used to modify the data.
    public void AddEntry(stuff);
    public void DeleteEntry(index);
    public void MoveEntry(to another category);
    public void AddCategory(stuff);
    public void DeleteCategory(index);
}
4

3 回答 3

4

您可以使用List<T>.AsReadOnly()返回ReadOnlyCollection<T>

此外,您正在折磨以List<T>您的方式存储数据的类。构建您自己的类层次结构来存储您的个人列表。

于 2016-03-01T19:18:58.997 回答
2

.NET集合应该支持covariance,但它们本身不支持(相反,一些接口支持 covariance https://msdn.microsoft.com/ru-ru/library/dd233059.aspx)。协方差意味着List<Conctrete>表现得像List<Base>ifConcrete的子类是 的子类Base。您可以使用接口协变或只使用这样的转换:

using System.Collections.Generic;

namespace MyApp
{
    interface IEntry
    {
    }

    class Entry : IEntry
    {
    }

    class Program
    {
        private List<List<Entry>> _matrix = null;

        public List<List<IEntry>> MatrixWithROElements
        {
            get 
            {
                return _matrix.ConvertAll(row => row.ConvertAll(item => item as IEntry));
            }
        }

        public IReadOnlyList<List<IEntry>> MatrixWithRONumberOfRows
        {
            get 
            {
                return _matrix.ConvertAll(row => row.ConvertAll(item => item as IEntry));
            }
        }

        public List<IReadOnlyList<IEntry>> MatrixWithRONumberOfColumns
        {
            get 
            {
                return _matrix.ConvertAll(row => row.ConvertAll(item => item as IEntry) as IReadOnlyList<IEntry>);
            }
        }

        public IReadOnlyList<IReadOnlyList<IEntry>> MatrixWithRONumberOfRowsAndColumns
        {
            get 
            {
                return _matrix.ConvertAll(row => row.ConvertAll(item => item as IEntry));
            }
        }

        public void Main(string[] args)
        {    
        }
    }
}

感谢Matthew Watson指出我之前的答案版本中的错误。

于 2016-03-01T19:54:08.633 回答
1

你可以创建一个Entry只包含 getter 的接口;您将通过此接口公开元素以提供只读访问权限:

public interface IEntry
{
    int Value { get; }
}

可写的实现很简单:

public sealed class Entry : IEntry
{
    public int Value { get; set; }
}

现在您可以利用这样一个事实,即您可以将 aList<List<Entry>>作为 a返回,IReadOnlyCollection<IReadOnlyCollection<IEntry>>而无需做任何额外的工作:

public sealed class Database
{
    private readonly List<List<Entry>> _list = new List<List<Entry>>();

    public Database()
    {
        // Create your list of lists.

        List<Entry> innerList = new List<Entry>
        {
            new Entry {Value = 1},
            new Entry {Value = 2}
        };

        _list.Add(innerList);
    }

    public IReadOnlyCollection<IReadOnlyCollection<IEntry>> Data => _list;
}

Data请注意该属性的实现是多么简单。

如果您需要向其中添加新属性,IEntry则还必须将它们添加到Entry,但您不需要更改Database类。

如果您使用的是 C#5 或更早版本,Data则如下所示:

public IReadOnlyCollection<IReadOnlyCollection<IEntry>> Data
{
    get { return _list; }
}
于 2016-03-01T19:38:57.040 回答