我正在创建一个像这样的自定义集合。自定义收集投掷错误的扩展方法
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?
当用LINQ迭代列表时,出现'NullReferenceException'通常意味着列表中的项目之一是'null' ...你检查过是不是这种情况? –
更新了代码。 – shashank
是什么失败是这条语句 this.ApplicationBrowser.Navigate(this.ExtensionObject.EndPoints.OrderBy(endpoint => endpoint.Order).First()。Url.AbsoluteUri); – shashank