【发布时间】:2014-01-24 20:06:54
【问题描述】:
上面的问题很模糊,让我详细说明一下。
在我的代码中,我设置了如下内容:
[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# 一年左右。
【问题讨论】:
-
您是否实现了
Equals或者您只是使用object中声明的引用相等检查的那个? (意味着只有相同的原始引用会等于它自己) -
是的,我正在使用
Object中的实现。我似乎发现了@user270576 的回答支持的问题。 -
顺便说一句,这个数据结构是周期表的表示吗?
-
不,它是光谱集中用于光谱分析的波段的表示。
-
@BenVoigt 我使用的关键字只是为了使示例更通用。不涉及周期性元素。
标签: c# winforms serialization protobuf-net