public object Clone() { InMemoryEntitySetDictionary inMemoryEntitySetDictionaryClone = new InMemoryEntitySetDictionary(); foreach (Type t in this.Keys) { IList newList = this.CloneList(this[t], t, this.GetTypes()); inMemoryEntitySetDictionaryClone.Add(t, newList); } this.HookupRelationshipsInClone(inMemoryEntitySetDictionaryClone, this.GetTypes()); return(inMemoryEntitySetDictionaryClone); }
private void HookupRelationshipsInClone(InMemoryEntitySetDictionary dictionary, Type[] entityTypesToHookup) { foreach (Type t in dictionary.Keys) { IList clonedList = dictionary[t]; IList originalList = this[t]; for (int i = 0; i < clonedList.Count; i++) { FixupEntityObject(originalList[i], clonedList[i], dictionary, entityTypesToHookup); } } }
public InMemoryEntitySetDictionary InitializeEntitySetDictionary() { //Create List of t's with the correct types InMemoryEntitySetDictionary inMemoryDictionary = new InMemoryEntitySetDictionary(); foreach (PropertyInfo info in this.GetType().GetProperties()) { if (typeof(IQueryable).IsAssignableFrom(info.PropertyType)) { Type t = info.PropertyType.GetGenericArguments()[0]; IList list = (IList)InMemoryEntitySetDictionary.CreateGeneric(typeof(List <>), t, null); inMemoryDictionary.Add(t, list); } } return(inMemoryDictionary); }
private void FixupEntityObject(object original, object cloned, InMemoryEntitySetDictionary dictionary, Type[] entityTypesToHookup) { //Reference properties foreach (PropertyInfo pi in original.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => entityTypesToHookup.Contains(p.PropertyType))) { IList list = dictionary.GetEntitySet(pi.PropertyType); object originalLinkedObject = pi.GetValue(original, null); object clonedLinkedObject = null; if (originalLinkedObject != null) { var key = InMemoryContext.GetKeyValues(originalLinkedObject); List <string> failedKeys = new List <string>(); foreach (object o in list) { var otherKey = InMemoryContext.GetKeyValues(o); if (InMemoryContext.EqualPropertyValues(key, otherKey)) { clonedLinkedObject = o; } else { failedKeys.Add(InMemoryContext.KeyToString(otherKey)); } } if (clonedLinkedObject == null) { throw new ApplicationException(String.Format( "Unable to find linked object with key '{0}' for reference property '{1}'. Other keys were: {2}", InMemoryContext.KeyToString(key), pi.Name, string.Join(", ", failedKeys.ToArray()))); } pi.SetValue(cloned, clonedLinkedObject, null); } } //Fix up for lists foreach (PropertyInfo pi in original.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.PropertyType.Name.Contains("List"))) { Type t = pi.PropertyType.GetGenericArguments()[0]; IList list = dictionary.GetEntitySet(t); IList originalListLinkedObject = (IList)pi.GetValue(original, null); IList clonedListLinkedObject = (IList)CreateGeneric(typeof(List <>), t, null); foreach (object originalItemInList in originalListLinkedObject) { object clonedLinkedObject = null; foreach (object o in list) { if (InMemoryContext.EqualKeys(o, originalItemInList)) { clonedLinkedObject = o; } } if (clonedLinkedObject == null) { throw new ApplicationException("Unable to find linked object for collection property '" + pi.Name + "'"); } clonedListLinkedObject.Add(clonedLinkedObject); } pi.SetValue(cloned, clonedListLinkedObject, null); } }