2013-12-13 86 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