C# 公开集合但排除部分

C# 公开集合但排除部分,c#,collections,list-template,C#,Collections,List Template,在父类中,我有一个集合。在子类中,我想公开父类集合的一部分。我希望任何一个位置的更改都会影响到另一个位置 我的实际情况是,我正在创建一个应用程序的一部分,用于记录数据库设计。我在数据库类中有一个ConstraintList集合。ConstraintList包含数据库中每个约束的约束类。我在数据库类中还有一个表列表集合,它包含表类。在表类中,我有一个ForeignKeyConstraintList,我想在其中公开父(数据库类)ConstraintList中的约束,这些约束是该表类的外键约束 +-D

在父类中,我有一个集合。在子类中,我想公开父类集合的一部分。我希望任何一个位置的更改都会影响到另一个位置

我的实际情况是,我正在创建一个应用程序的一部分,用于记录数据库设计。我在数据库类中有一个ConstraintList集合。ConstraintList包含数据库中每个约束的约束类。我在数据库类中还有一个表列表集合,它包含表类。在表类中,我有一个ForeignKeyConstraintList,我想在其中公开父(数据库类)ConstraintList中的约束,这些约束是该表类的外键约束

+-Database Class
|                            
+--ConstraintList  <-----------
|                             |
+--TableList              Same List
   |                          |
   +-Table Class              |
     |                        |
     +-ForeignKeyConstraintList
+-数据库类
|                            

+--ConstraintList我决定编写一个自定义列表类型类来完成此任务。我将在下面发布代码。我还没有测试过,但我想这对任何想做同样事情的人来说都是一个好的开始

嗯,看来这个班级太大了,不能在这里上课。我将只发布关键部分,跳过公共方法,它们只是实现各种接口

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace CodeWriter.Collections.Generic
{
    /// <summary>
    /// This represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort and manipulate the list.
    /// This class serves as a wrapper for a <see cref="List{T}"/>.  The internal class can be reached by the <see cref="SourceList"/> property.
    /// The elements that this class exposes from the <see cref="SourceList"/> can be controlled by changing the <see cref="Filter"/> property.
    /// </summary>
    /// <typeparam name="T">The type of elements in the list.</typeparam>
    /// <remarks>
    /// This class was created to support situations where the functionality of two or more <see cref="List{T}"/> collections are needed where one is the Master Collection
    /// and the others are Partial Collections.  The Master Collection is a <see cref="List{T}"/> and exposes all elements in the collection.  The Partial Collections 
    /// are <see cref="FilteredList{T}"/> classes (this class) and only expose the elements chosen by the <see cref="FilteredList{T}"/> property of this class.  When elements are modified,
    /// in either type of collection, the changes show up in the other collections because in the backend they are the same list.  When elements are added or deleted from the Partial Collections,
    /// they will disappear from the Master Collection.  When elements are deleted from the Master Collection, they will not be available in the Partial Collection but it 
    /// may not be apparent because the <see cref="Filter"/> property may not be exposing them.
    /// </remarks>
    public class FilteredList<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IList, ICollection, IReadOnlyList<T>, IReadOnlyCollection<T>
    {
        #region Public Constructor
        public FilteredList(List<T> SourceList)
        {
            if (SourceList == null)
            {
                throw new ArgumentNullException("SourceList");
            }

            _SourceList = SourceList;
        }

        public FilteredList()
        {
            _SourceList = new List<T>();
        }

        public FilteredList(IEnumerable<T> Collection)
        {
            if (Collection == null)
            {
                throw new ArgumentNullException("Collection");
            }

            _SourceList = new List<T>(Collection);
        }
        #endregion

        #region Protected Members
        protected List<T> _SourceList;
        protected Func<T, bool> _Filter;
        #endregion

        #region Public Properties
        #region Source List Properties
        /// <summary>
        /// Gets or sets the base class that this class is a wrapper around.
        /// </summary>
        public List<T> SourceList
        {
            get
            {
                return _SourceList;
            }
            set
            {
                _SourceList = value;
            }
        }

        /// <summary>
        /// Gets or sets the value used to filter the <see cref="SourceList"/>.
        /// </summary>
        public Func<T, bool> Filter
        {
            get
            {
                return _Filter;
            }
            set
            {
                _Filter = value;
            }
        }
        #endregion
        #region Normal List<T> Implementation
        /// <summary>
        /// Provides access to the collection the in the same manner as an <see cref="Array"/>.
        /// </summary>
        /// <param name="Index">The Index of the element you want to retrieve.  Valid values are from zero to the value in the <see cref="Count"/> property.</param>
        /// <returns>The element at the position provided with the indexer.</returns>
        public T this[int Index]
        {
            get
            {
                List<T> Selected = _SourceList.Where(_Filter).ToList();
                return Selected[Index];
            }
            set
            {
                List<T> Selected = _SourceList.Where(_Filter).ToList();
                Selected[Index] = value;
            }
        }

        /// <summary>
        /// Provides access to the collection the in the same manner as an <see cref="Array"/>.
        /// </summary>
        /// <param name="Index">The Index of the element you want to retrieve.  Valid values are from zero to the value in the <see cref="Count"/> property.</param>
        /// <returns>The element at the position provided with the indexer.</returns>
        /// <remarks>This is required for IList implementation.</remarks>
        object IList.this[int Index]
        {
            get
            {
                return this[Index];
            }
            set
            {
                if ((value is T) == false)
                {
                    throw new ArgumentException("Value passed is not a valid type.");
                }

                this[Index] = (T)value;
            }
        }

        /// <summary>
        /// Gets or sets the total number of elements the internal data structure can hold without resizing.
        /// </summary>
        public int Capacity
        {
            get
            {
                return _SourceList.Capacity;
            }
            set
            {
                // We cannot let them shrink capacity because this class is a wrapper for the List<T> in the _SourceList property.
                //  They don't get to see all the entries in that list because it is filtered.  Therefore it is not safe for them to shrink capacity.

                // We check if they are shrinking the capacity.
                if (value >= _SourceList.Capacity)
                {
                    _SourceList.Capacity = value;
                }
            }
        }

        /// <summary>
        /// Gets the number of elements contained in the <see cref="FilteredList{T}"/>.
        /// </summary>
        public int Count
        {
            get
            {
                List<T> Selected = _SourceList.Where(_Filter).ToList();

                return Selected.Count();
            }
        }

        /// <summary>
        /// Gets a value indicating whether the <see cref="FilteredList{T}"/> has a fixed size.
        /// </summary>
        public bool IsFixedSize
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the <see cref="FilteredList{T}"/> is read-only.
        /// </summary>
        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Gets a value indicating whether access to the <see cref="FilteredList{T}"/> is synchronized (thread safe).
        /// </summary>
        public bool IsSynchronized
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Gets an object that can be used to synchronize access to the <see cref="FilteredList{T}"/>.
        /// </summary>
        public object SyncRoot
        {
            get
            {
                return _SourceList;
            }
        }
        #endregion
        #endregion
    }
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Linq;
命名空间CodeWriter.Collections.Generic
{
/// 
///这表示可通过索引访问的对象的强类型列表。提供搜索、排序和操作列表的方法。
///此类用作的包装器。属性可以访问内部类。
///此类从中公开的元素可以通过更改属性来控制。
/// 
///列表中元素的类型。
/// 
///创建此类是为了支持需要两个或多个集合的功能的情况,其中一个集合是主集合
///其他的是部分集合。主集合是一个集合,并公开集合中的所有元素。部分集合
///是类(此类),仅公开由此类属性选择的元素。修改元素时,
///在任何一种类型的集合中,更改都会显示在其他集合中,因为在后端它们是相同的列表。当从部分集合中添加或删除元素时,
///它们将从主集合中消失。当从主集合中删除元素时,它们将在部分集合中不可用,但会被删除
///可能不明显,因为属性可能未暴露它们。
/// 
公共类筛选器列表:IList、ICollection、IEnumerable、IEnumerable、IList、ICollection、IReadOnlyList、IReadOnlyCollection
{
#区域公共构造函数
公共筛选器列表(列表源列表)
{
if(SourceList==null)
{
抛出新ArgumentNullException(“SourceList”);
}
_SourceList=SourceList;
}
公共筛选器列表()
{
_SourceList=新列表();
}
公共筛选器列表(IEnumerable集合)
{
if(集合==null)
{
抛出新的ArgumentNullException(“集合”);
}
_SourceList=新列表(集合);
}
#端区
#区域保护成员
受保护列表_SourceList;
受保护的Func_滤波器;
#端区
#区域公共财产
#区域源列表属性
/// 
///获取或设置该类作为包装的基类。
/// 
公共列表源列表
{
得到
{
返回源列表;
}
设置
{
_SourceList=值;
}
}
/// 
///获取或设置用于筛选的值。
/// 
公共函数过滤器
{
得到
{
返回过滤器;
}
设置
{
_过滤器=值;
}
}
#端区
#区域正常列表实现
/// 
///以与相同的方式提供对集合的访问。
/// 
///要检索的元素的索引。有效值从零到属性中的值。
///位于配有索引器的位置的元素。
公共T此[int索引]
{
得到
{
所选列表=_SourceList.Where(_Filter.ToList();
返回选中的[索引];
}
设置
{
所选列表=_SourceList.Where(_Filter.ToList();
所选[索引]=值;
}
}
/// 
///以与相同的方式提供对集合的访问。
/// 
///要检索的元素的索引。有效值从零到属性中的值。
///位于配有索引器的位置的元素。
///这是IList实现所必需的。
对象IList。此[int索引]
{
得到
{
返回此[索引];
}
设置
{
如果((值为T)=false)
{
抛出新ArgumentException(“传递的值不是有效类型”);
}
该[索引]=(T)值;
}
}
/// 
///获取或设置内部数据结构在不调整大小的情况下可以容纳的元素总数。
/// 
公共int容量
{
得到
{
返回_SourceList.Capacity;
}
设置
{
//我们不能