编辑:这应该从下一个版本开始工作,只需标记类型的AsReferenceDefault
:
[ProtoContract(AsReferenceDefault=true)]
public class A
{
// ...
}
目前这是sort of一个不受支持的场景 - 至少,通过属性它不受支持;基本上,AsReference=true
现在指的是KeyValuePair<int,A>
,这实际上没有意义,因为KeyValuePair<int,A>
是一个值类型(因此永远不能将其视为参考;我在本地副本中为此添加了更好的消息)。
Because KeyValuePair<int,A>
(默认情况下)充当tuple,目前没有地方支持AsReference
信息,但我想更好地支持这种情况,我将对此进行调查。
有also一个错误意味着AsReference
元组(甚至引用类型元组)上的问题变得无序,但我已经在本地修复了这个问题;这就是“已更改”消息的来源。
理论上,我做这件事的工作量并不大;这基础知识已经可以工作了,奇怪的是,昨晚它也在推特上单独出现——我想“字典指向一个对象”是一个非常常见的场景。据猜测,我想我会添加一些属性来帮助描述这种情况,但实际上您现在可以使用几种不同的路线来解决它:
1:配置KeyValuePair<int,A>
手动:
[Test]
public void ExecuteHackedViaFields()
{
// I'm using separate models **only** to keep them clean between tests;
// normally you would use RuntimeTypeModel.Default
var model = TypeModel.Create();
// configure using the fields of KeyValuePair<int,A>
var type = model.Add(typeof(KeyValuePair<int, A>), false);
type.Add(1, "key");
type.AddField(2, "value").AsReference = true;
// or just remove AsReference on Items
model[typeof(B)][2].AsReference = false;
Execute(model);
}
我不太喜欢这个,因为它利用了实现细节KeyValuePair<,>
(私有字段),并且可能无法在 .NET 版本之间工作。我更愿意replace KeyValuePair<,>
即时通过代理人:
[Test]
public void ExecuteHackedViaSurrogate()
{
// I'm using separate models **only** to keep them clean between tests;
// normally you would use RuntimeTypeModel.Default
var model = TypeModel.Create();
// or just remove AsReference on Items
model[typeof(B)][2].AsReference = false;
// this is the evil bit: configure a surrogate for KeyValuePair<int,A>
model[typeof(KeyValuePair<int, A>)].SetSurrogate(typeof(RefPair<int, A>));
Execute(model);
}
[ProtoContract]
public struct RefPair<TKey,TValue> {
[ProtoMember(1)]
public TKey Key {get; private set;}
[ProtoMember(2, AsReference = true)]
public TValue Value {get; private set;}
public RefPair(TKey key, TValue value) : this() {
Key = key;
Value = value;
}
public static implicit operator KeyValuePair<TKey,TValue>
(RefPair<TKey,TValue> val)
{
return new KeyValuePair<TKey,TValue>(val.Key, val.Value);
}
public static implicit operator RefPair<TKey,TValue>
(KeyValuePair<TKey,TValue> val)
{
return new RefPair<TKey,TValue>(val.Key, val.Value);
}
}
这配置了要使用的东西代替 KeyValuePair<int,A>
(通过运算符转换)。
在这两者中,Execute
只是:
private void Execute(TypeModel model)
{
A a = new A();
B b = new B();
b.A = a;
b.Items.Add(1, a);
Assert.AreSame(a, b.A);
Assert.AreSame(b.A, b.Items[1]);
B deserializedB = (B)model.DeepClone(b);
Assert.AreSame(deserializedB.A, deserializedB.Items[1]);
}
不过,我确实想添加直接支持。上述两者的好处是,当我有时间这样做时,您只需删除自定义配置代码即可。
为了完整起见,如果您的代码使用Serializer.*
方法,然后而不是创建/配置new模型,您应该配置default model:
RuntimeTypeModel.Default.Add(...); // etc
Serializer.*
基本上是一个捷径RuntimeTypeModel.Default.*
.
最后:你不应该创建一个新的TypeModel
每次通话;这会损害性能。您应该创建并配置一个模型实例,并多次重复使用它。或者只使用默认模型。