12

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

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

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

KeyValuePair<TKey, TValue>不是协变的。

相反,我必须这样做:

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

有没有更好/更清洁的方法?

4

2 回答 2

8

不幸的是,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;
    }
}
于 2013-02-16T14:52:04.287 回答
0

几乎不。KVP 是一个结构:不是 itnerface,是 ValueType。

有趣的关于方差的帖子。

我认为演员表的性能更高,所以我更喜欢这样的代码:

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

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

并投射KeyValuePair.ValueICollection我真正需要的地方。坦率地说,这取决于 foos 的使用方式。

于 2013-02-16T13:39:45.023 回答