目前正在开发一个通用的2向查找关联功能,按TKey排序。希望最终能够像下面这样方便地访问:
public class Assoc<TKey, TValue>
{
public TKey this[TValue value] { get; }
public TValue this[TKey value] { get; }
}
但是当TKey == TValue时,这将失败。出于好奇,是否有条件编译语法来实现这个:
public class Assoc<TKey, TValue>
{
[Condition(!(TKey is TValue))]
public TKey this[TValue value] { get; }
[Condition(!(TKey is TValue))]
public TValue this[TKey value] { get; }
public TKey Key(TValue value) { get; }
public TValue Value(TKey value) { get; }
}
public TKey this[TValue value] { get; }
是什么作用?我不理解这个 API 的目的。 - MatthewDictionary<string, string>
吗?看起来Jake想要一个非常类似于字典的东西,但是可以通过键和值进行关联搜索。 - Alexei Levenkovif value.GetType() == typeof(TKey)
这样的语句,并根据情况采取了相应的行动。但我想你已经猜到了替代方案... - Guillaume