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

发布于 2024-09-12 13:20:28 字数 1242 浏览 1 评论 0原文

在 RIA 服务中,EntityCollectionclass定义如下:

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

我有一个 Silverlight 转换器,它根据列表中的项目数量设置 Visibility

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

但是等等 - 我希望它对于任何 EntityCollection 都是通用的。 哦哦 - IEntityCollection 是内部的,我们无法访问。 EntityCollection 甚至没有实现 ICollection。

我是否在不使用反射的情况下陷入困境(我真的不想这样做,因为在某些情况下这可能会每秒被调用多次)。

我很确定我确实必须使用反射来使其通用 - 那么在这种情况下为什么 IEntityCollection 是内部的?监督?

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 cases).

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧幽蓝 2024-09-19 13:20:28

您可以自己实现该函数,而不是使用反射。您不关心计数,只关心它是否非零。只需重写 Enumberable.Any(IEnumerable) 函数即可获取非泛型 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(); 
} 

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(); 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文