public static bool EqualsByValue(this IMixin self, IMixin other) { return (self == null && other == null) || (self != null && other != null && self.GetMembers().Count() == other.GetMembers().Count() && self.GetMembers().All(name => Equals(self.GetProperty(name), other.GetProperty(name)))); }
public static bool EqualsByValue(this IMixin self, IMixin other) { return ((self == null && other == null) || (self != null && other != null && self.GetMembers().Count() == other.GetMembers().Count() && self.GetMembers().All(name => Equals(self.GetProperty(name), other.GetProperty(name))))); }
public static void SetProperty(this IMixin self, string name, object value) { EnsurePropertyName(ref name); if (Equals(value, self.GetProperty(name))) { return; } if (!StateChanging(self, name, value)) { return; // we can cancel state change } self.SetPropertyInternal(name, value); StateChanged(self, name, value); }
public static void MapTo(this IMapper self, IMixin destination, bool shapshot = false, bool deep = false) { if (shapshot) { var newProps = destination.GetMembers().Except(self.GetMembers()).ToList(); foreach (var prop in newProps) { destination.SetProperty(prop, self.GetPropertyType(prop).GetDefaultValue()); } } foreach (var name in self.GetMembers()) { var sourcePropType = self.GetPropertyType(name); var destPropType = destination.GetPropertyType(name); var sourceProperty = self.GetProperty(name); var destinationPropety = destination.GetProperty(name); // property if (sourceProperty is IMapper && deep) { var mapper = (IMapper)sourceProperty; if (destinationPropety == null && destPropType != null && typeof(IMixin).IsAssignableFrom(destPropType)) { destinationPropety = Activator.CreateInstance(destPropType); destination.SetProperty(name, destinationPropety); } if (destinationPropety is IMixin) { mapper.MapTo((IMixin)destinationPropety, shapshot, deep: true); } continue; } // list if (sourceProperty is IEnumerable <IMapper> && deep) { IList destinationList; if (destinationPropety == null && destPropType != null && typeof(IEnumerable <IMixin>).IsAssignableFrom(destPropType)) { destinationList = destPropType.CloneTypedList(); destination.SetProperty(name, destinationList); } else if (destinationPropety is IEnumerable <IMixin> ) { destinationList = (IList)destinationPropety; destinationList.Clear(); } else { continue; } var type = destinationList.GetType().GetGenericArguments().First(); foreach (var item in (IEnumerable <IMapper>)sourceProperty) { var destItem = (IMixin)Activator.CreateInstance(type); item.MapTo(destItem, shapshot, deep: true); destinationList.Add(destItem); } continue; } // simple property if (destPropType == null || sourcePropType == null || destPropType.IsAssignableFrom(sourcePropType)) // dynamic or compatible { destination.SetProperty(name, self.GetProperty(name)); } } }
public static dynamic GetValue(this IMixin self, [CallerMemberName] string name = null) { return(self.GetProperty(name)); }