我序列化了一些对象,所有对象都有一个"LinkObject“列表。这些列表共享了其中的一些“链接对象”。如果我反序列化,那么所有的列表元素将是独立的,不同的对象。
I have a list of "OperationsObjects":public class OperationObject
{
[XmlAttribute("Name")]
public string Name { get; set; }
public string Physname { get; set; }
public string JournalID { get; set; }
public List<ParameterObject> ParameterObjectList = new List<ParameterObject>();
public List<ConditionObject> ConditionObjectList = new List<ConditionObject>();
public List<LinkObject> ChildLinkObjectList = new List<LinkObject>();
}
public class LinkObject
{
public int? Number { get; set; }
public string LogicType { get; set; }
public string PrimaryID { get; set; }
public string SecondaryID { get; set; }
}如果我序列化,那么我得到了一些类似的XML行:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfOperationObjects xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<OperationObjectList>
<OperationObject Name="step forward">
<ParameterObjectList />
<ConditionObjectList />
<ChildLinkObjectList>
<LinkObject>
<Number>0</Number>
<LogicType>And</LogicType>
<PrimaryID>GanttOperationObject[gantt_1]</PrimaryID>
<SecondaryID>GanttOperationObject[gantt_2]</SecondaryID>
</LinkObject>
<LinkObject>
<Number>2</Number>
<LogicType>And</LogicType>
<PrimaryID>GanttOperationObject[gantt_3]</PrimaryID>
<SecondaryID>GanttOperationObject[gantt_1]</SecondaryID>
</LinkObject>
</ChildLinkObjectList>
<Physname>Program-Parameters</Physname>
<PhysType>NXOpen.Mechatronics.ProxyObject</PhysType>
<Duration>2</Duration>
<StartTime>1</StartTime>
<OperationType>Simple</OperationType>
<JournalID>GanttOperationObject[gantt_1]</JournalID>
</OperationObject>
<OperationObject Name="118 Component M8">
<ParameterObjectList />
<ConditionObjectList />
<ChildLinkObjectList>
<LinkObject>
<Number>0</Number>
<LogicType>And</LogicType>
<PrimaryID>GanttOperationObject[gantt_1]</PrimaryID>
<SecondaryID>GanttOperationObject[gantt_2]</SecondaryID>
</LinkObject>
<LinkObject>
<Number>1</Number>
<LogicType>And</LogicType>
<PrimaryID>GanttOperationObject[gantt_2]</PrimaryID>
<SecondaryID>GanttOperationObject[gantt_4]</SecondaryID>
</LinkObject>
</ChildLinkObjectList>
<Duration>1</Duration>
<StartTime>3</StartTime>
<OperationType>Simple</OperationType>
<JournalID>GanttOperationObject[gantt_2]</JournalID>
</OperationObject>因此,我希望LinkObjects有相同的对象,编号为0。实际上,我想编辑OperationObject Name=中的链接对象“前进一步”,我期望Linkobject也会在OperationObject Name="118 Component M8“中进行编辑。
发布于 2019-05-16 14:11:52
你不能自动这么做。但是您应该在反序列化之后手动执行。您可以收集所有唯一的LinkObjects来分离集合,并通过Id将每个相关的LinkObject替换为OperationObject (在您的示例中是Number值)。
有代码示例。我曾经使用过json序列化程序,但实际上它并不重要。
List<TestParent> parents = new List<TestParent>();
TestChild child = new TestChild() { Name = "Test" };
//add to parent class with the same child class;
parents.Add(new TestParent() { Child = new List<TestChild>() { child } });
parents.Add(new TestParent() { Child = new List<TestChild>() { child } });
String data = JsonConvert.SerializeObject(parents);
List<TestParent> deserializedData = JsonConvert.DeserializeObject<List<TestParent>>(data);
var comparer = new ChildComparer();
List<TestChild> brokenLinkCollection = deserializedData.SelectMany(x => x.Child).Distinct().ToList();
// 2 Child with the same Name
List<TestChild> uniqueCollection = deserializedData.SelectMany(x => x.Child).Distinct(comparer).ToList();
var processedChild = deserializedData.Select(x => x.Child).ToList();
processedChild.ForEach(x =>
{
var substitutedCollection = uniqueCollection.Where( uc => x.Contains(uc, comparer)).ToList();
x.Clear();
x.AddRange(substitutedCollection);
});
List<TestChild> resoredCollection = deserializedData.SelectMany(x => x.Child).Distinct().ToList();
// 1 Child is found due to linking to one memory object用过的课程:
class ChildComparer : EqualityComparer<TestChild>
{
public override bool Equals(TestChild b1, TestChild b2)
{
if (b1 == null && b2 == null)
return true;
else if (b1 == null || b2 == null)
return false;
return (b1.Name == b2.Name);
}
public override int GetHashCode(TestChild bx)
{
return bx.Name.GetHashCode();
}
}
public class TestChild
{
public string Name { set; get; }
}
public class TestParent
{
public List<TestChild> Child { set; get; }
}https://stackoverflow.com/questions/56170135
复制相似问题