开发者

Why is IEntityCollection internal / How to find EntityCollection<T>.Count?

In RIA services the EntityCollection<T> class is defined as follows :

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

I have a Silverlight converter which sets Visibility dependent upon the number of items in a list.

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

But wait - I want it to be generic for any EntityCollection. Uh oh - IEntityCollection is internal and not accessible to us. EntityCollection doesn't even implement ICollection.

Am I stuck without using reflection (which I really would rather not do since this may get called many times a second in some cas开发者_开发百科es).

I'm pretty sure I do have to use reflection to make this generic - so in that case why would IEntityCollection be internal? Oversight?


Rather than using reflection, you could just implement the function yourself. You don't care about the count, just that it's non-zero. Simply rewrite the Enumberable.Any(IEnumerable<T>) function to take non-generic IEnumerable:

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

    return source.GetEnumerator().MoveNext();
}

Then in your converter you would have:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜