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

标签 c# winforms serialization protobuf-net

上面的问题很模糊,所以让我详细说明一下。

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

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

相关文章:

c# - 隐藏 DataGridViewImageColumn 中的图像

c# - 文本框是否将全数字文本保存为长文本或字符串?

c# - 跟随/剪辑到形状的文本?

c# - Kendo.Mvc.dll 的 NuGet

c# - 清除 Windows 窗体中的所有文本框,特殊文本框除外

c# - 在 SerializationInfo 中获取具有值的成员

c# - 如何让 XamlReader.Parse 不抛出 "property does not exist"XamlParseException?

c# - 从 HttpResponse 读取特定的 div

c# - 以编程方式重命名计算机

c# - 使用自定义格式序列化 JSON 字符串