上述问题非常模糊,请允许我详细说明。
在我的代码中,我设置了类似的内容:
[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#。
答案 0 :(得分:2)
序列化/反序列化不保留对象引用。因此,在反序列化之后,每个对象都是新的。
答案 1 :(得分:2)
好的,所以我不太完全理解为什么我得到的结果。然而,我确实找到了解决这个问题的方法。我决定从所有父引用中删除[Protomember(n,AsReference = true)]
属性。然后,我构建了一个具有[ProtoAfterDeserializationAttribute]
属性的函数,该函数将遍历每个对象并使用this
关键字分配每个父引用。这将确保对象具有相同的参考。
答案 2 :(得分:0)
我意识到线程现在已经老了,但是以下代码对我有效。 AsReference = true表示它存储为引用,如下所示,每个“Parent”对象都存储为单个对象,而不是新实例。请参阅调试器监视窗口。
library(RCurl)
# check current IP address
print(getURL("http://ifconfig.me/ip"))
# proxy options
opts <- list(proxy="127.0.0.1", proxyport=8118)
# opening connection with TOR
con <- socketConnection(host="127.0.0.1",port=9051)
print(getURL("http://ifconfig.me/ip", .opts = opts))
for (i in 1:10)
{
writeLines('AUTHENTICATE \"password\"\r\nSIGNAL NEWNYM\r\n', con=con)
Sys.sleep(5)
print(getURL("http://ifconfig.me/ip", .opts = opts))
Sys.sleep(5)
}
//测试代码
[ProtoContract]
public class Parent
{
[ProtoMember(1)]
public List<Child> Children
{
get { return m_Children; }
set { m_Children = value; }
}
private List<Child> m_Children = new List<Child>();
}
[ProtoContract]
public class Child
{
[ProtoMember(1, AsReference = true)]
public Parent Parent
{
get { return m_Parent; }
set { m_Parent = value; }
}
[ProtoMember(2)]
public int Index
{
get { return m_Index; }
set { m_Index = value; }
}
Parent m_Parent;
int m_Index;
}