KeyValuePair 协变性

13

在这个例子中是否有更好的方法来模拟协方差?理想情况下,我想做到:

private IDictionary<string, ICollection<string>> foos;

public IEnumerable<KeyValuePair<string, IEnumerable<string>> Foos
{
    get
    {
        return foos;
    }
}
< p >但是KeyValuePair<TKey, TValue>不是协变的。

相反,我必须这样做:

public IEnumerable<KeyValuePair<string, IEnumerable<string>>> Foos
{
    get
    {
        return foos.Select(x => 
            new KeyValuePair<string, IEnumerable<string>>(x.Key, x.Value));
    }
}

有更好/更干净的方法吗?

2个回答

10

不幸的是,KeyValuePair<TKey, TValue> 是一个结构体;而结构体在.NET中没有表现出变化。

当然,您可以通过编写自己的协变 Pair 接口并编写一些简单的帮助程序来转换 KeyValuePair 序列和您的自定义 Pair 接口之间的序列来解决此问题。 这将使您能够执行以下操作:

var dict = new Dictionary<string, ICollection<string>>();

var view = dict.GetCovariantView(); // IEnumerable< IPair<string, ICollection<string> > >
  
// Notice that you can _widen_ both the key and the value types:
var dictView = view.CastPairs<object, IEnumerable<string>>(); // IEnumerable< IPair< object, IEnumerable<String> > >

// The `CastPairs` call is actually unnecessary provided you don't use `var` for the left-hand-side assignment.
// ...this is due to the implicit (and identity-preserving) variant interface conversion in C#, e.g.:
IEnumerable< IPair< Object, IEnumerable<String> > > dictView2 = view;

Console.WriteLine( Object.ReferenceEquals( view, dictView2 ) ); // --> True

以下是一些示例代码,可以帮助您实现此功能:
// `out TKey` is for demonstration purposes. In production-quality code you probably should be using invariant key types.
public interface IPair<out TKey, out TValue>
    where TKey : notnull
{
    TKey   Key   { get; }
    TValue Value { get; }
}

public class Pair<TKey, TValue> : IPair<TKey, TValue>
    where TKey : notnull
{
    public TKey   Key   { get; }
    public TValue Value { get; }

    public Pair(TKey key, TValue value)
    {
        this.Key   = key;
        this.Value = value;
    }

    public Pair(KeyValuePair<TKey, TValue> pair)
        : this(pair.Key, pair.Value)
    {}
}

public static class PairSequenceExtensions
{
    public static IEnumerable<IPair<TKey, TValue>> GetCovariantView<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source)
        where TKey : notnull
    {
        if (source is null) throw new ArgumentNullException(nameof(source));

        return source.Select(kvp => new Pair<TKey, TValue>(kvp));
    }

    public static IEnumerable<IPair<TKey, TValue>> CastPairs<TKey, TValue>(this IEnumerable<IPair<TKey, TValue>> source)
        where TKey : notnull
    {
        if (source is null) throw new ArgumentNullException(nameof(source));

        return source;
    }
}

0

很少。KVP是一个结构体:不是接口,是值类型。

关于变异的SO post有趣的帖子。

我认为强制转换更具性能,所以我更喜欢这样编码:

private IDictionary<string, IEnumerable<string>> foos;

public IEnumerable<KeyValuePair<string, IEnumerable<string>> Foos
{
    get
    {
        return foos;
    }
}

并将 KeyValuePair.Value 强制转换为 ICollection,如果我真的需要的话。坦白地说,这取决于 foos 的使用方式。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接