【问题标题】:What is the implementing class for IGrouping?IGrouping 的实现类是什么?
【发布时间】:2011-12-14 16:01:05
【问题描述】:

我正在尝试创建一个 WCF 数据服务 ServiceOperation,它在服务器端进行分组,然后将数据向下发送到客户端。

当我尝试调用它(甚至连接到服务)时,我收到了一个错误。它说它不能构造一个接口。

我使用的唯一接口是 IGrouping。

这个接口的实际类是什么?


更新:

我在调试示例应用程序时检查了类型,它告诉我它是:

System.Linq.Lookup<TKey,TElement>.Grouping

但它在哪个程序集中?

【问题讨论】:

标签: c# .net wcf-data-services


【解决方案1】:

BCL 中的几个类型实现了IGrouping,但它们都是内部的,只能通过IGrouping 接口访问。

但是IGrouping 只是一个带有关联键的IEnumerable&lt;TElement&gt;。您可以轻松实现由List&lt;TElement&gt; 支持的IGrouping,并且跨调用边界序列化应该不难:

public class Grouping<TKey, TElement> : IGrouping<TKey, TElement> {

  readonly List<TElement> elements;

  public Grouping(IGrouping<TKey, TElement> grouping) {
    if (grouping == null)
      throw new ArgumentNullException("grouping");
    Key = grouping.Key;
    elements = grouping.ToList();
  }

  public TKey Key { get; private set; }

  public IEnumerator<TElement> GetEnumerator() {
    return this.elements.GetEnumerator();
  }

  IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }

}

应用GroupBy 运算符后,您可以创建Grouping 实例列表:

var listOfGroups =
  source.GroupBy(x => ...).Select(g => new Grouping<TKey, TElement>(g)).ToList();

【讨论】:

  • 可能最简单的方法是这样的: var newGrouping = new List().GroupBy(t => t.MyKey);
  • @aBetterGamer:我认为问题不在于如何创建IGrouping,正如您所指出的那样,这很容易,而是如何重新创建在另一边实现IGrouping 的“东西”的序列化边界。
  • 答案帮助我了解IGrouping&lt;key,value&gt; 如何在内部产生多个值 +1。
【解决方案2】:

这可能是 IGrouping 最基本和最通用的实现。它的构造函数接受一个键和一组值。

public class Grouping<TKey, TElement> : IGrouping<TKey, TElement>
{
    private readonly TKey key;
    private readonly IEnumerable<TElement> values;

    public Grouping(TKey key, IEnumerable<TElement> values)
    {
        if (values == null)
            throw new ArgumentNullException("values");
        this.key = key;
        this.values = values;
    }

    public TKey Key
    {
        get { return key; }
    }

    public IEnumerator<TElement> GetEnumerator()
    {
        return values.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

【讨论】:

    【解决方案3】:

    我使用示例应用检查了类型,它是:System.Linq.Lookup&lt;TKey,TElement&gt;.Grouping。但它在哪个程序集中?

    这是一个嵌套在System.Linq.Lookup&lt;TKey,TElement&gt;中的类型; System.Core 程序集的内部。

     var groupingType = "1".GroupBy(x => x).Single().GetType().GetGenericTypeDefinition();
     Console.WriteLine("Type: " + groupingType);
     Console.WriteLine("Public: " + groupingType.IsPublic);
     Console.WriteLine("Assembly: " + groupingType.Assembly);
    

    输出:

    Type: System.Linq.Lookup`2+Grouping[TKey,TElement]
    Public: False
    Assembly: System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    

    从 .NET 4.0 开始,核心 .NET 框架中没有实现 System.Linq.IGrouping&lt;TKey,TElement&gt; 的公共类型。如果你需要这样的类型(比如说它是可序列化的),很遗憾,你可能不得不自己滚动一个。

    【讨论】:

      猜你喜欢
      • 2010-10-05
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      • 2019-06-02
      • 2020-03-23
      相关资源
      最近更新 更多