/// <summary> /// take all the property with the same name and type /// </summary> /// <param name="source_content">content target</param> /// <param name="target_content">content source</param> /// <remarks></remarks> public static void copyTo(this IContent source_content, IContent target_content) { if (source_content == null || target_content == null) { return; } Type source_type = source_content.GetType(); Type target_type = target_content.GetType(); IEnumerable <ContentProperty> all = source_type.getMysteryPropertyAttributes <ContentProperty>(); HashSet <ContentProperty> source_properties = new HashSet <ContentProperty>((from x in all where x.retrive != null select x)); all = target_type.getMysteryPropertyAttributes <ContentProperty>(); HashSet <ContentProperty> target_properties = new HashSet <ContentProperty>((from x in all where x.retrive != null select x)); Dictionary <string, MysteryPropertyAttribute> source_map = new Dictionary <string, MysteryPropertyAttribute>(); foreach (ContentProperty sp in source_properties) { source_map[sp.name] = sp; } Dictionary <string, MysteryPropertyAttribute> target_map = new Dictionary <string, MysteryPropertyAttribute>(); foreach (ContentProperty tp in target_properties) { target_map[tp.name] = tp; } foreach (string pn in target_map.Keys) { try { if (!source_map.ContainsKey(pn)) { continue; } MysteryPropertyAttribute source = source_map[pn]; MysteryPropertyAttribute target = target_map[pn]; if ((!object.ReferenceEquals(source.used_in.PropertyType, target.used_in.PropertyType))) { continue; } target.save(target_content, source.retrive(source_content)); } catch { //well if some failed is not the end of the world, is it? } } }
/// <summary> /// compare all the properties between target content and source content. /// </summary> /// <param name="source_content">content target</param> /// <param name="target_content">content source</param> /// <remarks></remarks> public static bool samePropertiesValue(this IContent source_content, IContent target_content) { if (source_content == null && target_content == null) { return(true); } if (source_content == null || target_content == null) { return(false); } Type source_type = source_content.GetType(); Type target_type = target_content.GetType(); IEnumerable <ContentProperty> all = source_type.getMysteryPropertyAttributes <ContentProperty>(); HashSet <ContentProperty> source_properties = new HashSet <ContentProperty>((from x in all where x.retrive != null select x)); all = target_type.getMysteryPropertyAttributes <ContentProperty>(); HashSet <ContentProperty> target_properties = new HashSet <ContentProperty>((from x in all where x.retrive != null select x)); Dictionary <string, MysteryPropertyAttribute> source_map = new Dictionary <string, MysteryPropertyAttribute>(); foreach (ContentProperty sp in source_properties) { source_map[sp.name] = sp; } foreach (ContentProperty tp in target_properties) { try { if (!source_map.ContainsKey(tp.name)) { continue; } MysteryPropertyAttribute source = source_map[tp.name]; if ((!object.ReferenceEquals(source.used_in.PropertyType, tp.used_in.PropertyType))) { continue; } object source_value = source.retrive(source_content); object target_value = tp.retrive(target_content); if (source_value == null && target_value == null) { continue; } //hating datetime a little more every day, when we store them in a db or js or anything on their way //back they might differe a little, equals compare the ticks so it is would have to be quite close. //in case of date we assume the same value if they differ for less than 1ms. if (source_value is DateTime && target_value is DateTime && source_value != null && target_value != null) { if (((DateTime)source_value - (DateTime)target_value).TotalMilliseconds < 1) { continue; } } //one of them is not null, so what to use Equals class methods if (source_value != null && !source_value.Equals(target_value)) { return(false); } if (target_value != null && !target_value.Equals(source_value)) { return(false); } } catch { return(false); } } return(true); }