gpt4 book ai didi

c# - protobuf-net - 为什么反序列化对象后引用同一对象的对象不相等

转载 作者:行者123 更新时间:2023-11-30 20:51:51 31 4
gpt4 key购买 nike

上面的问题很含糊,让我详细说说。

在我的代码中,我设置了如下内容:

[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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com