public virtual void WriteObjRefs() { IObjRef[] allOris = new IObjRef[4]; ObjRef ori = new ObjRef(typeof(String), 2, 4); allOris[0] = ori; String xml = CyclicXmlHandler.Write(ori); Assert.AssertEquals(XmlTestConstants.XmlOutput[15], xml, "Wrong xml"); Object actual = CyclicXmlHandler.Read(xml); Assert.AssertSame(typeof(ObjRef), actual.GetType(), "Wrong class"); AssertObjRefEquals(ori, (ObjRef)actual); ori = new ObjRef(typeof(String), -1, 2, 4); allOris[1] = ori; xml = CyclicXmlHandler.Write(ori); Assert.AssertEquals(XmlTestConstants.XmlOutput[15], xml, "Wrong xml"); actual = CyclicXmlHandler.Read(xml); Assert.AssertSame(typeof(ObjRef), actual.GetType(), "Wrong class"); AssertObjRefEquals(ori, (ObjRef)actual); ori = new ObjRef(typeof(String), 0, "zwei", 4); allOris[2] = ori; xml = CyclicXmlHandler.Write(ori); Assert.AssertEquals(XmlTestConstants.XmlOutput[16], xml, "Wrong xml"); actual = CyclicXmlHandler.Read(xml); Assert.AssertSame(typeof(ObjRef), actual.GetType(), "Wrong class"); AssertObjRefEquals(ori, (ObjRef)actual); ori = new ObjRef(typeof(String), 1, "zwei", 4); allOris[3] = ori; xml = CyclicXmlHandler.Write(ori); Assert.AssertEquals(XmlTestConstants.XmlOutput[17], xml, "Wrong xml"); actual = CyclicXmlHandler.Read(xml); Assert.AssertSame(typeof(ObjRef), actual.GetType(), "Wrong class"); AssertObjRefEquals(ori, (ObjRef)actual); xml = CyclicXmlHandler.Write(allOris); actual = CyclicXmlHandler.Read(xml); Assert.AssertEquals(allOris.GetType(), actual.GetType()); IObjRef[] actualArray = (IObjRef[])actual; Assert.AssertEquals(allOris.Length, actualArray.Length); for (int i = 0; i < allOris.Length; i++) { AssertObjRefEquals((ObjRef)allOris[i], (ObjRef)actualArray[i]); } }
protected void ScanForInitializedObjectsIntern(Object obj, bool isDeepMerge, IList <Object> objects, IMap <Type, IList <Object> > typeToObjectsToMerge, ISet <Object> alreadyHandledObjectsSet, IList <IObjRef> objRefs, IList <ValueHolderRef> valueHolderKeys) { if (obj == null || !alreadyHandledObjectsSet.Add(obj)) { return; } if (obj is IList) { IList list = (IList)obj; for (int a = 0, size = list.Count; a < size; a++) { ScanForInitializedObjectsIntern(list[a], isDeepMerge, objects, typeToObjectsToMerge, alreadyHandledObjectsSet, objRefs, valueHolderKeys); } return; } else if (obj.GetType().IsArray) { Array array = (Array)obj; for (int a = array.Length; a-- > 0;) { Object item = array.GetValue(a); ScanForInitializedObjectsIntern(item, isDeepMerge, objects, typeToObjectsToMerge, alreadyHandledObjectsSet, objRefs, valueHolderKeys); } return; } else if (obj is IEnumerable && !(obj is String)) { foreach (Object item in (IEnumerable)obj) { ScanForInitializedObjectsIntern(item, isDeepMerge, objects, typeToObjectsToMerge, alreadyHandledObjectsSet, objRefs, valueHolderKeys); } return; } IEntityMetaData metaData = EntityMetaDataProvider.GetMetaData(obj.GetType(), true); if (metaData == null) { return; } IObjRef objRef = null; Object id = metaData.IdMember.GetValue(obj, false); if (id != null) { objRef = ObjRefFactory.CreateObjRef(metaData.EntityType, ObjRef.PRIMARY_KEY_INDEX, id, null); objRef.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic); } if (!(obj is IDataObject) || ((IDataObject)obj).HasPendingChanges) { if (typeToObjectsToMerge != null) { IList <Object> objectsToMerge = typeToObjectsToMerge.Get(metaData.EntityType); if (objectsToMerge == null) { objectsToMerge = new List <Object>(); typeToObjectsToMerge.Put(metaData.EntityType, objectsToMerge); } objectsToMerge.Add(obj); } objects.Add(obj); objRefs.Add(objRef); } if (!isDeepMerge) { return; } RelationMember[] relationMembers = metaData.RelationMembers; if (relationMembers.Length == 0) { return; } IObjRefContainer vhc = (IObjRefContainer)obj; for (int relationIndex = relationMembers.Length; relationIndex-- > 0;) { if (ValueHolderState.INIT != vhc.Get__State(relationIndex)) { continue; } RelationMember relationMember = relationMembers[relationIndex]; Object item = relationMember.GetValue(obj); if (objRef != null && item != null) { ValueHolderRef vhk = new ValueHolderRef(objRef, relationMember, relationIndex); valueHolderKeys.Add(vhk); } ScanForInitializedObjectsIntern(item, isDeepMerge, objects, typeToObjectsToMerge, alreadyHandledObjectsSet, objRefs, valueHolderKeys); } }