- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 ILookup<int, Derived>
我想返回 ILookup<int, Base>
其中 Derived
实现或扩展 Base
.
目前我使用SelectMany()
然后 ToLookup()
首先提取ILookup<int, Derived>
的键值对进公寓IEnumerable
然后创建一个新的 ILookup<int, Base>
:
class Base { }
class Derived: Base { }
class Test
{
ILookup<int, Base> CastLookup(ILookup<int, Derived> existing)
{
IEnumerable<KeyValuePair<int, Base>> flattened = existing.SelectMany(
(x) => x,
(gr, v) => new KeyValuePair<int, Base>(gr.Key, (Base)v)); // I know the explicit cast can be implicit here; it is just to demonstrate where the up casting is happening.
ILookup<int, Base> result = flattened.ToLookup(
(x) => x.Key,
(x) => x.Value);
return result;
}
}
如何在不迭代其条目然后重新打包它们的情况下转换 ILookup?
注意:一个相关的问题是 Shouldn't ILookup<TKey, TElement> be (declared) covariant in TElement?通过 bigge . Ryszard Dżegan answers这主要是出于历史原因:ILookup<TKey, TElement>
在具有协变性的泛型之前已经被开发出来。
Herzmeister问something similar对于 Dictionary<TKey, TValue>
. Mehrdad Afshari answers对于可变字典,协方差是不安全的。
事实上,如果 Ilookup<TKey, TElement>
在 TElement
中是协变的, 我不会遇到 ILookup<TKey, TElement>
的这个实例选角问题;但事实并非如此,所以我仍在继续寻求更好的方法。
注意:我当然可以编写扩展方法来执行此操作,但这并不能阻止所需的迭代和重新打包计算工作。
最佳答案
你可以创建一个代理:
public static ILookup<TKey, TValueBase> ToLookupBase<TKey, TValue, TValueBase>(this ILookup<TKey, TValue> lookup)
where TValue : class, TValueBase
{
return new LookupProxy<TKey, TValue, TValueBase>(lookup);
}
public class LookupProxy<TKey, TValue, TValueBase> : ILookup<TKey, TValueBase>
where TValue : class, TValueBase
{
private readonly ILookup<TKey, TValue> lookup;
public LookupProxy(ILookup<TKey, TValue> lookup)
{
this.lookup = lookup;
}
public IEnumerable<TValueBase> this[TKey key] => lookup[key];
public int Count => lookup.Count;
public bool Contains(TKey key) => lookup.Contains(key);
public IEnumerator<IGrouping<TKey, TValueBase>> GetEnumerator() => lookup.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
请注意,您必须:
var base = existing.ToLookupBase<int, Derived, Base>();
如此显式地告诉所有泛型参数。如果你甚至想支持 TKey
的协方差,那就有点复杂了,需要一个单独的支持类和一个单独的方法:
public static ILookup<TKeyBase, TValueBase> ToLookupBase2<TKey, TValue, TKeyBase, TValueBase>(ILookup<TKey, TValue> lookup)
where TKey : class, TKeyBase
where TValue : class, TValueBase
{
return new LookupProxy2<TKey, TValue, TKeyBase, TValueBase>(lookup);
}
public class LookupProxy2<TKey, TValue, TKeyBase, TValueBase> : ILookup<TKeyBase, TValueBase>
where TKey : class, TKeyBase
where TValue : class, TValueBase
{
private readonly ILookup<TKey, TValue> lookup;
public LookupProxy2(ILookup<TKey, TValue> lookup)
{
this.lookup = lookup;
}
public IEnumerable<TValueBase> this[TKeyBase key] => key is TKey ? lookup[(TKey)key] : Enumerable.Empty<TValueBase>();
public int Count => lookup.Count;
public bool Contains(TKeyBase key) => key is TKey ? lookup.Contains((TKey)key) : false;
public IEnumerator<IGrouping<TKeyBase, TValueBase>> GetEnumerator() => lookup.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
这是因为您需要添加一个 where TKey : class, TKeyBase
(它不支持键的值类型,就像您的示例中那样)。
关于c# - 如何转换查找的 TElement;在。 Lookup<TKey, Derived> 到 ILookup<TKey, Base>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51397920/
我实现了一个字典如下: Dictionary dictionary; 我已经定义了Equals()和 GetHashCode()在ErrorHashKey类(class)。我目前正在为该项目编写一些文
IList除了 SortedList.Keys 不支持的操作外,接口(interface)还包括按索引访问属性如 Add , Remove , 和 Insert . A ReadOnlyCollect
我有一个 Enumerable> .我想创建一个 bool TryGetValue(TKey, out TValue)它的扩展方法就像它在 Dictionary 中可用一样. 我试过了 public
只是覆盖 TKey 中的 Equals 没有帮助。 public override bool Equals(object obj) { /* ... */ } ... Equals() 永远不会被调
作为MSDN says ConcurrentDictionary Class 表示一个线程安全的键值对集合,可以被多个线程同时访问。 但据我所知,System.Collections.Concurre
我一直无法阐明 ILookup 之间的差异和 IGrouping ,并且很好奇我现在是否理解正确。 LINQ 通过生成 IGrouping 的序列使问题更加复杂项目同时还给我 ToLookup扩展方法
这似乎是这个 question 的副本,它会问“SortedList 和 SortedDictionary 有什么区别?”不幸的是,答案只是引用了 MSDN 文档(其中明确指出两者之间存在性能和内存使
我正在尝试创建自定义 ReadOnlyDictionary对于.NET 4.0。方法是保持私有(private) Dictionary对象以及标志以确定是否允许添加/删除和项目分配。 这很好用,但我想
我想这并不重要,我只是好奇。 如果字典和查找之间的区别是一个是一对一的,另一个是一对多的,那么不会通过另一个更具体/派生的版本来字典吗? 查找是键/值对的集合,其中键可以重复。字典是键/值对的集合,其
我想通过例子来理解TSource,Tkey的概念。 我们有代码 class Pet { public string Name { get; se
我想在给定 Dictionary 的情况下获取 TKey 和 TValue 的类型类型。 例如。如果类型是 Dictionary我想知道如何获得keyType = typeof(Int32) 和val
我想在给定 Dictionary 的情况下获取 TKey 和 TValue 的类型类型。 例如。如果类型是 Dictionary我想知道如何获得keyType = typeof(Int32) 和val
好的 - 所以我知道构建一个提供功能的工厂方法很简单;但鉴于 Dictionary是 IEnumerable> ,它不应该有一个等价于例如List 的Ctor吗?的ctor(IEnumerable r
我有一个 ILookup我想返回 ILookup其中 Derived实现或扩展 Base . 目前我使用SelectMany()然后 ToLookup()首先提取ILookup的键值对进公寓IEnum
我正在尝试存储 Linq 所需的表达式 OrderBy数据结构中的子句,所以我可以去query = query.OrderBy(MySortExpression); OrderBy需要 System.
我有一个返回 IDictionary > 的函数. 我有另一个函数需要 IDictionary > . 我需要将第一个函数的返回传递给第二个函数。 编译器不想将第一个隐式转换为第二个。那么如何在 O(
我正在尝试构建一个通用存储库并使用 autofac 进行测试。我有以下界面: public interface IGenRepo where T : class { IQueryable It
我有一个字典如下: public enum Role { Role1, Role2, Role3, } public enum Action { Action1, Action2, Action3,
我认为转换 IDictionary> 相当简单反对 IDictionary> , 但是 var val = (IDictionary>)Value; 抛出 System.InvalidCastExce
我有一个 IDictionary TKey在哪里是一个类。我可以添加将索引器与 String 一起使用的能力吗?值而不是 TKey ? public class MyClass { public
我是一名优秀的程序员,十分优秀!