Ahhhh, it's nice to be appreciated sometimes - even though I'm still feel like a bit of a Microsoft foreigner in the SAP world -
http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/40dbb54f-5c28-2d10-ad94-f812a548dc07
The Musings and Findings of Software Consultant David Klein (Sydney, Australia)
////// Shallow Property Example /// /// /// public static void CopyPropertyValues(object source, object destination) { var destProperties = destination.GetType().GetProperties(); foreach (var sourceProperty in source.GetType().GetProperties()) { foreach (var destProperty in destProperties) { if (destProperty.Name == sourceProperty.Name && destProperty.PropertyType.IsAssignableFrom(sourceProperty.PropertyType)) { destProperty.SetValue(destination, sourceProperty.GetValue( source, new object[] { }), new object[] { }); break; } } } }
using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; ////// Provides a method for performing a deep copy of an object. /// Binary Serialization is used to perform the copy. /// public static class ObjectCopier { ////// Perform a deep Copy of the object. /// ///The type of object being copied. /// The object instance to copy.///The copied object. public static T Clone(T source) { if (!typeof(T).IsSerializable) { throw new ArgumentException("The type must be serializable.", "source"); } // Don't serialize a null object, simply return the default for that object if (Object.ReferenceEquals(source, null)) { return default(T); } IFormatter formatter = new BinaryFormatter(); Stream stream = new MemoryStream(); using (stream) { formatter.Serialize(stream, source); stream.Seek(0, SeekOrigin.Begin); return (T)formatter.Deserialize(stream); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace DDKOnline.Common { ////// Utility /// public static class DeepCopyUtility { ////// Copies the data of one object to another. The target object gets properties of the first. /// Any matching properties (by name) are written to the target. /// /// The source object to copy from/// The target object to copy topublic static void CopyObjectData(object source, object target) { CopyObjectData(source, target, String.Empty, BindingFlags.Public | BindingFlags.Instance); } ////// Copies the data of one object to another. The target object gets properties of the first. /// Any matching properties (by name) are written to the target. /// /// The source object to copy from/// The target object to copy to/// A comma delimited list of properties that should not be copied/// Reflection binding accesspublic static void CopyObjectData(object source, object target, string excludedProperties, BindingFlags memberAccess) { string[] excluded = null; if (!string.IsNullOrEmpty(excludedProperties)) { excluded = excludedProperties.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries); } MemberInfo[] miT = target.GetType().GetMembers(memberAccess); foreach (MemberInfo Field in miT) { string name = Field.Name; // Skip over excluded properties if (string.IsNullOrEmpty(excludedProperties) == false && excluded.Contains(name)) { continue; } if (Field.MemberType == MemberTypes.Field) { FieldInfo sourcefield = source.GetType().GetField(name); if (sourcefield == null) { continue; } object SourceValue = sourcefield.GetValue(source); ((FieldInfo)Field).SetValue(target, SourceValue); } else if (Field.MemberType == MemberTypes.Property) { PropertyInfo piTarget = Field as PropertyInfo; PropertyInfo sourceField = source.GetType().GetProperty(name, memberAccess); if (sourceField == null) { continue; } if (piTarget.CanWrite && sourceField.CanRead) { object targetValue = piTarget.GetValue(target, null); object sourceValue = sourceField.GetValue(source, null); if (sourceValue == null) { continue; } if (sourceField.PropertyType.IsArray && piTarget.PropertyType.IsArray && sourceValue != null) { CopyArray(source, target, memberAccess, piTarget, sourceField, sourceValue); } else { CopySingleData(source, target, memberAccess, piTarget, sourceField, targetValue, sourceValue); } } } } } private static void CopySingleData(object source, object target, BindingFlags memberAccess, PropertyInfo piTarget, PropertyInfo sourceField, object targetValue, object sourceValue) { //instantiate target if needed if (targetValue == null && piTarget.PropertyType.IsValueType == false && piTarget.PropertyType != typeof(string)) { if (piTarget.PropertyType.IsArray) { targetValue = Activator.CreateInstance(piTarget.PropertyType.GetElementType()); } else { targetValue = Activator.CreateInstance(piTarget.PropertyType); } } if (piTarget.PropertyType.IsValueType == false && piTarget.PropertyType != typeof(string)) { CopyObjectData(sourceValue, targetValue, "", memberAccess); piTarget.SetValue(target, targetValue, null); } else { if (piTarget.PropertyType.FullName == sourceField.PropertyType.FullName) { object tempSourceValue = sourceField.GetValue(source, null); piTarget.SetValue(target, tempSourceValue, null); } else { CopyObjectData(piTarget, target, "", memberAccess); } } } private static void CopyArray(object source, object target, BindingFlags memberAccess, PropertyInfo piTarget, PropertyInfo sourceField, object sourceValue) { int sourceLength = (int)sourceValue.GetType().InvokeMember("Length", BindingFlags.GetProperty, null, sourceValue, null); Array targetArray = Array.CreateInstance(piTarget.PropertyType.GetElementType(), sourceLength); Array array = (Array)sourceField.GetValue(source, null); for (int i = 0; i < array.Length; i++) { object o = array.GetValue(i); object tempTarget = Activator.CreateInstance(piTarget.PropertyType.GetElementType()); CopyObjectData(o, tempTarget, "", memberAccess); targetArray.SetValue(tempTarget, i); } piTarget.SetValue(target, targetArray, null); } } }
var duplicates = timesheetDto.TimesheetItems.Where(timesheetItem => timesheetDto.TimesheetItems .Except(new List{ timesheetItem }) .Any(matchingTimesheetItem => timesheetItem.ActivityType == matchingTimesheetItem.ActivityType && timesheetItem.ReceivingWBSElement == matchingTimesheetItem.ReceivingWBSElement && timesheetItem.ReceivingCostCentre == matchingTimesheetItem.ReceivingCostCentre && timesheetItem.SendingCostCentre == matchingTimesheetItem.SendingCostCentre ) ).ToList(); return duplicates.Count > 0;