Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实现IList facade,但GetEnumerator返回0的计数_C# - Fatal编程技术网

C# 实现IList facade,但GetEnumerator返回0的计数

C# 实现IList facade,但GetEnumerator返回0的计数,c#,C#,我对.NET编程有点陌生,有一点问题让我很困惑 我试图在我的一个类中实现IList接口。现在我想让事情简单一些,所以我只是使用了方法声明的列表字段的功能,并在两者之间添加了一些自定义数据处理 大多数方法都运行良好,字段列表按预期添加到。但是,当我尝试将该类用作列表时(例如,class.ForEach()),它返回一个0的计数。尽管内部列表的计数为25,但仍然存在这种情况 我相信这很简单,如果这个问题浪费了数据库空间,我很抱歉,但这确实是一个阻碍我前进的问题。你们谁能帮我解决这个问题 我将发布我当

我对.NET编程有点陌生,有一点问题让我很困惑

我试图在我的一个类中实现
IList
接口。现在我想让事情简单一些,所以我只是使用了方法声明的
列表
字段的功能,并在两者之间添加了一些自定义数据处理

大多数方法都运行良好,字段列表按预期添加到。但是,当我尝试将该类用作列表时(例如,
class.ForEach()
),它返回一个0的
计数。尽管内部
列表
的计数为25,但仍然存在这种情况

我相信这很简单,如果这个问题浪费了数据库空间,我很抱歉,但这确实是一个阻碍我前进的问题。你们谁能帮我解决这个问题

我将发布我当前的代码,以显示我正在尝试执行的操作:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;
using ClubEventsData;
using ClubEventsData.Attributes;
using ClubEventsDataHandling.System.Implementations.Conversion;
using Telerik.Sitefinity.DynamicModules.Model;

namespace ClubEventsDataHandling.System.Implementations
{
    public class DataRepository<T> : IDataRepository<T> where T : ClubEventData, new()
    {
        private readonly PropertyDictionaryBasedDynamicConverter<T> _converter;
        private readonly IDataHandler _dataHandler;
        private readonly List<T> _storageList;

        /// <summary>
        /// Initializes a new instance of the DataRepository class.
        /// </summary>
        public DataRepository()
        {
            _dataHandler = new SitefinityBasedDataHandler<T>();
            _converter = new PropertyDictionaryBasedDynamicConverter<T>();
            _storageList = new List<T>();

            // Populate the data repository.
            foreach (DynamicContent dynamicContent in _dataHandler.Get())
            {
                _storageList.Add(_converter.ConvertToModel(dynamicContent));
            }
        }

        #region Implementation of IEnumerable

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public IEnumerator<T> GetEnumerator()
        {
            return _storageList.GetEnumerator();
        }

        #endregion

        #region Implementation of ICollection<T>

        /// <summary>
        /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
        /// </summary>
        /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
        public void Add(T item)
        {
            // Create the data item.
            T modelItem = CreateDataItem(item);

            // Add the item to the list.
            _storageList.Add(modelItem);
        }

        /// <summary>
        /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
        /// </summary>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only. </exception>
        public void Clear()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"/> contains a specific value.
        /// </summary>
        /// <returns>
        /// true if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false.
        /// </returns>
        /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
        public bool Contains(T item)
        {
            // Determine if the database contains the item.
            return _dataHandler.Get(item.MasterContentID) != null;
        }

        /// <summary>
        /// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"/> to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index.
        /// </summary>
        /// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param><param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param><exception cref="T:System.ArgumentNullException"><paramref name="array"/> is null.</exception><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="arrayIndex"/> is less than 0.</exception><exception cref="T:System.ArgumentException">The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"/> is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.</exception>
        public void CopyTo(T[] array, int arrayIndex)
        {
            _storageList.CopyTo(array, arrayIndex);
        }

        /// <summary>
        /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
        /// </summary>
        /// <returns>
        /// true if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>.
        /// </returns>
        /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
        public bool Remove(T item)
        {
            // Determine if the master content ID has been set.
            ValidateItem(item);

            // Delete the item from the database.
            _dataHandler.Delete(item.MasterContentID);

            // Remove the item from the database.
            return _storageList.Remove(item);
        }

        /// <summary>
        /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
        /// </summary>
        /// <returns>
        /// The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
        /// </returns>
        public int Count { get; private set; }

        /// <summary>
        /// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
        /// </summary>
        /// <returns>
        /// true if the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only; otherwise, false.
        /// </returns>
        public bool IsReadOnly { get; private set; }

        /// <summary>
        /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"/> contains a specific value.
        /// </summary>
        /// <returns>
        /// true if <paramref name="itemID"/>'s item is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false.
        /// </returns>
        /// <param name="itemID">The ID of the object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
        public bool Contains(Guid itemID)
        {
            return _dataHandler.Get(itemID) != null;
        }

        ///<summary>Creates the data item in the database and returns the model version of it.</summary>
        private T CreateDataItem(T item)
        {
            // Get the properties of the item.
            Dictionary<string, object> propertyDictionary = GetItemProperties(item);

            // Create the data representation of the item.
            Guid createdItemID = _dataHandler.Create(propertyDictionary);

            // Convert a new model item from the dynamic content.
            DynamicContent dynamicContent = _dataHandler.Get(createdItemID);
            T modelItem = _converter.ConvertToModel(dynamicContent);
            return modelItem;
        }

        ///<summary>Gets the properties of the item as a dictionary that is ready to input into data handler methods.</summary>
        private static Dictionary<string, object> GetItemProperties(T item)
        {
            var propertyDictionary = new Dictionary<string, object>();

            // Filter  the properties according to the mapping attribute.
            foreach (PropertyInfo property in item.GetType().GetProperties())
            {
                // Get the mapping attribute.
                object[] customMappingAttributes = property.GetCustomAttributes(typeof (ClubEventDataMappingAttribute), true);
                var mappingAttribute = customMappingAttributes[0] as ClubEventDataMappingAttribute;

                // Apply the action, depending on the mapping attribute.
                if (mappingAttribute != null && mappingAttribute.MappingEnabled)
                {
                    string mappingValue = mappingAttribute.MappingName != null && string.IsNullOrEmpty(mappingAttribute.MappingName) ? property.Name : mappingAttribute.MappingName;
                    if (mappingValue != null)
                    {
                        propertyDictionary.Add(mappingValue, property.GetValue(item, null));
                    }
                }

                // Old code, in case didn't work.
                /*if ()
                {
                    propertyDictionary.Add(property.Name, property.GetValue(item, null));
                }*/
            }

            return propertyDictionary;
        }

        /// <summary>
        /// Performs validation operations on the item.
        /// </summary>
        private static void ValidateItem(T item)
        {
            // Check that the ID is present on the item.
            if (item.MasterContentID == Guid.Empty)
            {
                throw new ContentIDNotSetException("The master content ID was not set for this exception.");
            }
        }

        #endregion

        #region Implementation of IList<T>

        /// <summary>
        /// Determines the index of a specific item in the <see cref="T:System.Collections.Generic.IList`1"/>.
        /// </summary>
        /// <returns>
        /// The index of <paramref name="item"/> if found in the list; otherwise, -1.
        /// </returns>
        /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.IList`1"/>.</param>
        public int IndexOf(T item)
        {
            return _storageList.IndexOf(item);
        }

        /// <summary>
        /// Inserts an item to the <see cref="T:System.Collections.Generic.IList`1"/> at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param><param name="item">The object to insert into the <see cref="T:System.Collections.Generic.IList`1"/>.</param><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.</exception><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1"/> is read-only.</exception>
        public void Insert(int index, T item)
        {
            // Create a new data item based off the properties of this one.
            T modelItem = CreateDataItem(item);

            // Insert the model item into the list.
            _storageList.Insert(index, modelItem);
        }

        /// <summary>
        /// Removes the <see cref="T:System.Collections.Generic.IList`1"/> item at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index of the item to remove.</param><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.</exception><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.IList`1"/> is read-only.</exception>
        public void RemoveAt(int index)
        {
            // Get the item at the index.
            T item = this[index];

            // Validate the item ID.
            ValidateItem(item);

            // Delete the item from the database.
            _dataHandler.Delete(item.MasterContentID);

            // Remove the item from the list.
            _storageList.RemoveAt(index);
        }

        /// <summary>
        /// Gets or sets the element at the specified index.
        /// </summary>
        /// <returns>
        /// The element at the specified index.
        /// </returns>
        /// <param name="index">The zero-based index of the element to get or set.</param><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.</exception><exception cref="T:System.NotSupportedException">The property is set and the <see cref="T:System.Collections.Generic.IList`1"/> is read-only.</exception>
        public T this[int index]
        {
            get { return _storageList[index]; }
            set
            {
                // Validate the specified content ID.
                ValidateItem(value);

                // Update the database instance with the item properties.
                _dataHandler.Update(value.MasterContentID, GetItemProperties(value));

                // Reflect the changes in the list.
                _storageList[index] = value;
            }
        }

        #endregion

        #region Implementation of IEnumerable

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion
    }
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
运用系统反思;
使用ClubEventsData;
使用ClubEventsData.Attributes;
使用ClubEventsDataHandling.System.Implementations.Conversion;
使用Telerik.Sitefinity.DynamicModules.Model;
命名空间ClubEventsDataHandling.System.Implementations
{
公共类数据存储库:IDataRepository,其中T:ClubEventData,new()
{
私有只读属性DictionaryBaseDdynamicConverter\u converter;
私有只读IDataHandleru数据处理器;
私有只读列表\u存储列表;
/// 
///初始化DataRepository类的新实例。
/// 
公共数据存储库()
{
_dataHandler=new SitefinityBasedDataHandler();
_converter=新属性DictionaryBasedDynamicConverter();
_storageList=新列表();
//填充数据存储库。
foreach(DynamicContent DynamicContent在_dataHandler.Get()中)
{
_添加(_converter.ConvertToModel(dynamicContent));
}
}
#IEnumerable的区域实现
/// 
///返回遍历集合的枚举数。
/// 
/// 
///可用于在集合中迭代的。
/// 
/// 1
公共IEnumerator GetEnumerator()
{
返回_storageList.GetEnumerator();
}
#端区
#ICollection的区域实现
/// 
///将项目添加到列表中。
/// 
///要添加到的对象。是只读的。
公共作废新增(T项)
{
//创建数据项。
T modelItem=CreateDataItem(项目);
//将项目添加到列表中。
_storageList.Add(modelItem);
}
/// 
///从中删除所有项目。
/// 
///该文件是只读的。
公共空间清除()
{
抛出新的NotImplementedException();
}
/// 
///确定是否包含特定值。
/// 
/// 
///如果在中找到,则为true;否则为false。
/// 
///要在中定位的对象。
公共布尔包含(T项)
{
//确定数据库是否包含该项。
return _dataHandler.Get(item.MasterContentID)!=null;
}
/// 
///从特定索引开始,将的元素复制到。
/// 
///作为从中复制的元素的目标的一维索引。必须具有从零开始的索引。从中开始复制的从零开始的索引为空。小于0。源中的元素数大于从到目标末尾的可用空间。
public void CopyTo(T[]数组,int arrayIndex)
{
_CopyTo(数组,arrayIndex);
}
/// 
///从中删除特定对象的第一个引用。
/// 
/// 
///如果已成功从中删除,则为true;否则为false。如果未在原始中找到,则此方法也返回false。
/// 
///要从中删除的对象。是只读的。
公共布尔删除(T项)
{
//确定是否已设置主内容ID。
验证项(项目);
//从数据库中删除该项。
_dataHandler.Delete(item.MasterContentID);
//从数据库中删除该项。
返回存储列表。删除(项);
}
/// 
///获取中包含的元素数。
/// 
/// 
///中包含的元素数。
/// 
公共整数计数{get;私有集;}
/// 
///获取一个值,该值指示是否为只读。
/// 
/// 
///如果为只读,则为true;否则为false。
/// 
公共bool是只读的{get;private set;}
/// 
///确定是否包含特定值。
/// 
/// 
///如果在中找到的项,则为true;否则为false。
/// 
///要在中定位的对象的ID。
公共bool包含(Guid itemID)
{
return _dataHandler.Get(itemID)!=null;
}
///在数据库中创建数据项并返回其模型版本。
私有T CreateDataItem(T项)
{
//获取项目的属性。
Dictionary propertyDictionary=GetItemProperties(项目);
//创建项目的数据表示形式。
Guid createdItemID=\u dataHandler.Create(propertyDictionary);
//从动态内容转换新模型项。
动态内容动态内容
using System.Collections.Generic;
using ClubEventsData;
using System.Linq;
using System;

namespace ClubEventsDataHandling.System.Implementations.Controllers
{
    public class TicketDataController : IDataController
    {
        private DataRepository<Ticket> _repository;

        public TicketDataController()
        {
            _repository = new DataRepository<Ticket>();
        }

        public List<Ticket> GetDateTickets(Guid dateID)
        {
            return _repository.Where(ticket => ticket.EventDateBoughtFor.MasterContentID == dateID).ToList();
        }
    }
}
public int Count
{ 
    get { return _storageList.Count; }
}