上面的问题很模糊,所以让我详细说明一下。
在我的代码中,我设置了如下内容:
[ProtoContract]
[ProtoInclude(50, typeof(SubGroup))]
public class BaseGroup
{
[ProtMember(1)]
List<BaseElement> elements;
}
[ProtoContract]
public class SubGroup : BaseGroup
{
//Some protomembers
}
[ProtoContract]
[ProtoInclude(100, typeof(Set))]
public class BaseElement
{
[ProtoMember(1, AsReference = true)]
BaseGroup Parent;
}
[ProtoContract]
public class Set : BaseElement
{
//some protomembers here
[ProtoMember(1)]
List<Band> bands;
}
[ProtoContract]
public class Band
{
//some protomembers here
[ProtoMember(1, AsReference = true)]
Set Parent;
}
现在,在代码的另一部分中,我执行如下操作:
public void Function(Band b)
{
Set parentSet = b.Parent;
SubGroup parentGroup = (SubGroup)parentSet.Parent;
foreach(Set s in parentGroup.elements)
{
if(!s.Equals(parentSet))
{
//This section of code is skipped when references s and parentSet are equal.
//I then save to file by serializing the entire Basegroup, I
//then deserialize back into a BaseGroup object.
//Once deserialized, this function is called and this part of the code
//is executed meaning the objects with supposedly the same reference
//are not equal anymore.
//I performed this test with only one Set object meaning only one object in
//in the List of elements in the BaseGroup object
}
}
}
我希望我的解释是正确的。我只从事 C# 一年左右。
最佳答案
序列化/反序列化不保留对象引用。因此,反序列化后每个对象都是新的。
关于c# - protobuf-net - 为什么反序列化对象后引用同一对象不相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21341546/