public class Flea : Animals {...}
var fleas = new Dictionary<int, Flea>();
public IReadOnlyDictionary<string, Animal> Animals => fleas.ToDictionary(pair => pair.Key, pair => (Animal)pair.Value);Q有没有一种更有效的从fleas获取Animals的方法
发布于 2019-06-14 10:38:11
.NET支持接口、委托、泛型类型和数组中的协变性。接口或类型必须指定它的协变量,尽管使用out关键字。
你可以写
IEnumerable<Animal> animals=new List<Flea>();或
var dict=new Dictionary<int,Flea>{
[1]=new Flea()
};
IEnumerable<Animal> animals=dict.Values;这是因为Dictionary.Values返回一个IEnumerable<Flea>,而IEnumerable是协变的-它的定义是IEnumerable<out T>。
不过,KeyValuePair并不是协变的,这意味着使用它的类(如IDictionary<TKey,TValue>和IReadOnlyDictionary<TKey,TValue> )也不是。这是故意的。
因为您只需要从该字典中读取,所以可以使用委托创建一个访问器方法,或者在C# 7和更高版本中创建一个本地函数。您可以将该函数传递给期望使用Func<TKey,TValue>的方法,并使用它从字典中读取值。
如果您有一个需要基于密钥的访问的方法,那么假设:
void Process(Func<int,Animal> reader)
{
var value=reader(1);
}在C# 7中,您可以编写:
var dict =...
Animal get(int key)=>dict[key];
Process(get);通过使用变量捕获来访问字典,这有点作弊。
在C# 7之前,您需要使用一个委托:
Func<int,Animal> get= key=>dict[key];
Process(get);这可能看起来很奇怪,但这就是LINQ本身的工作方式,它使用谓词和委托,而不是接口和包装器。
发布于 2019-06-14 09:46:15
.NET框架不包含支持向上转换的字典包装器,但是实现一个非常简单:
public class ReadOnlyDictionaryUpcast<TKey, TValueDerived, TValueBase>
: IReadOnlyDictionary<TKey, TValueBase> where TValueDerived : TValueBase
{
private readonly Dictionary<TKey, TValueDerived> _dictionary;
public ReadOnlyDictionaryUpcast(Dictionary<TKey, TValueDerived> dictionary)
{
_dictionary = dictionary;
}
public int Count => _dictionary.Count;
public TValueBase this[TKey key] => _dictionary[key];
public bool ContainsKey(TKey key) => _dictionary.ContainsKey(key);
public bool TryGetValue(TKey key, out TValueBase value)
{
bool result = _dictionary.TryGetValue(key, out TValueDerived valueDerived);
value = valueDerived;
return result;
}
public IEnumerator<KeyValuePair<TKey, TValueBase>> GetEnumerator() => _dictionary
.Select(e => new KeyValuePair<TKey, TValueBase>(e.Key, e.Value))
.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public IEnumerable<TKey> Keys => _dictionary.Keys;
public IEnumerable<TValueBase> Values =>
(IEnumerable<TValueBase>)(IEnumerable<TValueDerived>)_dictionary.Values;
}用法示例:
var animals = new ReadOnlyDictionaryUpcast<string, Flea, Animal>(fleas);https://stackoverflow.com/questions/56593508
复制相似问题