I serialized some objects, all objects have a list of "LinkObject". These Lists are sharing some of these "Linkobjects". If I deserialize then all list element will be independent, different object.
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; }
}
If I serialize then I got some similar XML lines:
<?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>
So I want to have the same Object for LinkObjects with Number 0. Actually I want to edit the Linkobject in my OperationObject Name="step forward", and My expectation that the LinkObject will be also edited in OperationObject Name="118 Component M8"
You can not do it automatically. But you should do it manually after deserialization. You can collect all unique LinkObjects to separate collection and substitute each related LinkObject to OperationObject from this collection by Id (in your case it is Number value).
There is code example. I have used json serializer but actually it does not matter.
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
used classes:
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; }
}