这里的问题是假设反序列化器调用了setter——或者完全调用setter,或者完全在你期望的时候;它不是必需的。序列化程序假定了相当典型的实现,在这种情况下,以下是完全合理的:
// when field 2
var val = obj.OffsetDictionary;
bool setValue = false;
if (val == null)
{
val = new SortedDictionary<short, SortedDictionary<short, uint>>();
setValue = true;
}
do {
val.Add(/* parse this entry */);
} while (/* still field 2 */)
if (setValue) obj.OffsetDictionary = val;
尽管请注意,在开始时分配(在哪里setValue
分配)也是合法的。
碰巧的是,您可以通过使用
[ProtoMember(2, OverwriteList = true)]
但是......它的工作原理是错误的IMO,因为它也可以使用与上面相同的伪代码来实现,但只需添加 a .Clear()
,这不会改变输出。
坦率地说,我不确定我是否真的喜欢这里的整体设计。就个人而言,我会将数据保留在您要序列化的形式中,并在其上添加实用方法Test
,即
[ProtoContract]
public class Test
{
[ProtoMember(1)]
public DateTime BaseDate { get; set; }
[ProtoMember(2)]
public SortedDictionary<short, SortedDictionary<short, uint>> OffsetDictionary { get; }
= new SortedDictionary<short, SortedDictionary<short, uint>>();
private short ToInt16(DateTime value) => (short)(value - BaseDate).TotalDays;
public void Add(DateTime key, SortedDictionary<short, uint> value)
=> OffsetDictionary.Add(ToInt16(key), value);
public bool TryGetValue(DateTime key, out SortedDictionary<short, uint> value)
=> OffsetDictionary.TryGetValue(ToInt16(key), out value);
}
但是,您也可以使用包装层来执行此操作 - 尽管它需要做更多的工作:
[ProtoContract]
public class Test
{
[ProtoMember(1)]
public DateTime BaseDate { get; set; }
private DictionaryWrapper _offsetDictionary;
[ProtoMember(2)]
public IDictionary<short, SortedDictionary<short, uint>> OffsetDictionary
=> _offsetDictionary ?? (_offsetDictionary = new DictionaryWrapper(this));
public SortedDictionary<DateTime, SortedDictionary<short, uint>> Dictionary { get; }
= new SortedDictionary<DateTime, SortedDictionary<short, uint>>();
class DictionaryWrapper : IDictionary<short, SortedDictionary<short, uint>>
{
public DictionaryWrapper(Test parent)
{
_parent = parent;
}
private readonly Test _parent;
private DateTime ToDateTime(short value) => _parent.BaseDate.AddDays(value);
private short ToInt16(DateTime value) => (short)(value - _parent.BaseDate).TotalDays;
SortedDictionary<short, uint> IDictionary<short, SortedDictionary<short, uint>>.this[short key]
{
get => _parent.Dictionary[ToDateTime(key)];
set => _parent.Dictionary[ToDateTime(key)] = value;
}
int ICollection<KeyValuePair<short, SortedDictionary<short, uint>>>.Count => _parent.Dictionary.Count;
bool ICollection<KeyValuePair<short, SortedDictionary<short, uint>>>.IsReadOnly => false;
void IDictionary<short, SortedDictionary<short, uint>>.Add(short key, SortedDictionary<short, uint> value)
=> _parent.Dictionary.Add(ToDateTime(key), value);
void ICollection<KeyValuePair<short, SortedDictionary<short, uint>>>.Add(KeyValuePair<short, SortedDictionary<short, uint>> item)
=> _parent.Dictionary.Add(ToDateTime(item.Key), item.Value);
void ICollection<KeyValuePair<short, SortedDictionary<short, uint>>>.Clear()
=> _parent.Dictionary.Clear();
private ICollection<KeyValuePair<DateTime, SortedDictionary<short, uint>>> AsCollection => _parent.Dictionary;
bool ICollection<KeyValuePair<short, SortedDictionary<short, uint>>>.Contains(KeyValuePair<short, SortedDictionary<short, uint>> item)
=> AsCollection.Contains(new KeyValuePair<DateTime, SortedDictionary<short, uint>>(ToDateTime(item.Key), item.Value));
bool IDictionary<short, SortedDictionary<short, uint>>.ContainsKey(short key)
=> _parent.Dictionary.ContainsKey(ToDateTime(key));
private IEnumerator<KeyValuePair<short, SortedDictionary<short, uint>>> GetEnumerator()
{
foreach (var item in _parent.Dictionary)
yield return new KeyValuePair<short, SortedDictionary<short, uint>>(ToInt16(item.Key), item.Value);
}
IEnumerator<KeyValuePair<short, SortedDictionary<short, uint>>> IEnumerable<KeyValuePair<short, SortedDictionary<short, uint>>>.GetEnumerator()
=> GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
bool IDictionary<short, SortedDictionary<short, uint>>.Remove(short key)
=> _parent.Dictionary.Remove(ToDateTime(key));
bool ICollection<KeyValuePair<short, SortedDictionary<short, uint>>>.Remove(KeyValuePair<short, SortedDictionary<short, uint>> item)
=> AsCollection.Remove(new KeyValuePair<DateTime, SortedDictionary<short, uint>>(ToDateTime(item.Key), item.Value));
bool IDictionary<short, SortedDictionary<short, uint>>.TryGetValue(short key, out SortedDictionary<short, uint> value)
=> _parent.Dictionary.TryGetValue(ToDateTime(key), out value);
// these are kinda awkward to implement
ICollection<short> IDictionary<short, SortedDictionary<short, uint>>.Keys
=> throw new NotSupportedException();
ICollection<SortedDictionary<short, uint>> IDictionary<short, SortedDictionary<short, uint>>.Values
=> throw new NotSupportedException();
void ICollection<KeyValuePair<short, SortedDictionary<short, uint>>>.CopyTo(KeyValuePair<short, SortedDictionary<short, uint>>[] array, int arrayIndex)
=> throw new NotSupportedException();
}
}