2013-12-13 55 views
0

我正在创建一个像这样的自定义集合。自定义收集投掷错误的扩展方法

public class ClientBusinessEntityCollection<T> : ICollection<T> where T : EntityBase 
{ 
    /// <summary> 
    /// The list business objects 
    /// </summary> 
    private List<T> listBusinessObjects = null; 

    /// <summary> 
    /// Initializes a new instance of the <see cref="KddiBusinessEntityCollection{T}"/> class. 
    /// </summary> 
    public ClientBusinessEntityCollection() 
    { 
     this.listBusinessObjects = new List<T>(); 
    } 

    /// <summary> 
    /// Initializes a new instance of the <see cref="KddiBusinessEntityCollection{T}"/> class. 
    /// </summary> 
    /// <param name="collection">The collection.</param> 
    public ClientBusinessEntityCollection(IEnumerable<T> collection) 
    { 
     this.listBusinessObjects = new List<T>(collection); 
    } 

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

    /// <summary> 
    /// Gets the <see cref="`0"/> at the specified index. 
    /// </summary> 
    /// <param name="index">The index.</param> 
    /// <returns>`0.</returns> 
    public T this[long index] 
    { 
     get 
     { 
      return this.listBusinessObjects[(int)index]; 
     } 
    } 

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

    /// <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> 
    public void Add(T item) 
    { 
     this.listBusinessObjects.Add(item); 
    } 

    /// <summary> 
    /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1" />. 
    /// </summary> 
    public void Clear() 
    { 
     this.listBusinessObjects.Clear(); 
    } 

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

    /// <summary> 
    /// Sorts the collection. 
    /// </summary> 
    /// <param name="sorter">The sorter.</param> 
    public void SortCollection(Func<EntityBase, object> sorter) 
    { 
     //// TODO : IMPLEMENT SORTING HERE. 
    } 

    /// <summary> 
    /// Copies to. 
    /// </summary> 
    /// <param name="array">The array.</param> 
    /// <param name="arrayIndex">Index of the array.</param> 
    public void CopyTo(T[] array, int arrayIndex) 
    { 
    } 

    /// <summary> 
    /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1" />. 
    /// </summary> 
    /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1" />.</param> 
    /// <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> 
    public bool Remove(T item) 
    { 
     return this.listBusinessObjects.Remove(item); 
    } 

    /// <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> 
    public IEnumerator<T> GetEnumerator() 
    { 
     return this.listBusinessObjects.GetEnumerator(); 
    } 

    /// <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> 
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 
    { 
     return this.listBusinessObjects.GetEnumerator(); 
    } 
} 

现在我有这样

public ClientBusinessEntityCollection<MyClass> Collection {get; set;} 

集合现在的问题是,当我写LINQ声明“宝典”它抛出空引用异常。

Collection.OrderBy(item=>item.Order); 

收集具有潜在的名单,但不像列表,当你将鼠标悬停我自定义的“收集”它不显示的项目数。在我的自定义集合上编写LINQ时,如何从底层List对象中进行扩展方法拾取值?

我是否需要编写自定义IEnumerator?

+1

当用LINQ迭代列表时,出现'NullReferenceException'通常意味着列表中的项目之一是'null' ...你检查过是不是这种情况? –

+0

更新了代码。 – shashank

+0

是什么失败是这条语句 this.ApplicationBrowser.Navigate(this.ExtensionObject.EndPoints.OrderBy(endpoint => endpoint.Order).First()。Url.AbsoluteUri); – shashank

回答

0

你需要通过到您的收藏做出您的基础列表中的任何枚举相关的调用,如下所示:

public class MyTest<T> : ICollection<T> 
{ 
    private List<T> myUnderlyingList = new List<T>(); 

    public IEnumerator<T> GetEnumerator() 
    { 
     return myUnderlyingList.GetEnumerator(); 
    } 

    IEnumerator IEnumerable.GetEnumerator() 
    { 
     return GetEnumerator(); 
    } 

.... etc. 

然后LINQ将只是“看”的基础列表时执行运算。

另一件事是确保您的底层List<T>正确填充有效数据,然后再对其运行任何LINQ方法。

+0

感谢您的回答,但正如我所说,我已经在ICollection 上实施了所有方法,这就是其中之一。 – shashank

+0

好的!在这种情况下,你可以发布你的实现吗?如果执行有问题,那可能是你错误的根源。 – Baldrick

+0

我如何在这里发布整个代码? – shashank