代码之家  ›  专栏  ›  技术社区  ›  Simon_Weaver

为什么EntityCollection是内部的/如何查找EntityCollection<T>.Count?

  •  2
  • Simon_Weaver  · 技术社区  · 14 年前

    在RIA服务中 EntityCollection<T> class 定义如下:

    public sealed class EntityCollection<TEntity> : IEntityCollection, 
                                                    IEnumerable<TEntity>, 
                                                    IEnumerable,  
                                                    INotifyCollectionChanged,  
                                                    INotifyPropertyChanged where TEntity :  
                                                    global::System.ServiceModel.DomainServices.Client.Entity
    

    Visibility

     if (value is EntityCollection<CustomerFeedbackDetail>)
     {
          visible = (value as EntityCollection<CustomerFeedbackDetail>).Count > 0;
     }
    

    但是等等-我希望它对任何EntityCollection都是通用的。 哦哦- IEntityCollection

    我是否被困在没有使用反射的情况下(我真的不想这样做,因为在某些情况下,一秒钟可能会调用很多次)。

    我很确定我必须使用反射来实现这个通用性-那么在这种情况下为什么要这样做呢 客户端集合 是内部的吗?监督?

    1 回复  |  直到 14 年前
        1
  •  3
  •   Gabe Timothy Khouri    14 年前

    与其使用反射,不如自己实现函数。你不在乎计数,只是它不是零。只需重写 Enumberable.Any(IEnumerable<T>) IEnumerable :

    public static bool Any(this System.Collections.IEnumerable source)
    {
        if (source == null)
            throw new ArgumentNullException("source");
    
        return source.GetEnumerator().MoveNext();
    }
    

    在转换器中,您将拥有:

    if (value is EntityCollection<CustomerFeedbackDetail>) 
    { 
        visible = (value as IEnumerable).Any(); 
    }