//[OnDeserialized] //private void OnDeserialized(StreamingContext context) //{ // RebindEvents(); //} protected virtual void ChildCollectionChanged(object?sender, NotifyCollectionChangedEventArgs args) { string?propertyName = AttributeManager <NotifyCollectionChangedAttribute> .FindPropertyName(this, sender !); if (propertyName != null) { NotifyPrivate(propertyName); } if (AttributeManager <NotifyChildPropertyAttribute> .FieldsWithAttribute(this).Contains(sender)) { if (args.NewItems != null) { foreach (var p in args.NewItems.Cast <ModifiableEntity>()) { p.SetParentEntity(this); } } if (args.OldItems != null) { foreach (var p in args.OldItems.Cast <ModifiableEntity>()) { p.SetParentEntity(this); } } } }
protected virtual void RebindEvents() { foreach (INotifyCollectionChanged?notify in AttributeManager <NotifyCollectionChangedAttribute> .FieldsWithAttribute(this)) { if (notify == null) { continue; } notify.CollectionChanged += ChildCollectionChanged; } foreach (object?field in AttributeManager <NotifyChildPropertyAttribute> .FieldsWithAttribute(this)) { if (field == null) { continue; } if (field is ModifiableEntity entity) { entity.SetParentEntity(this); } else { foreach (var item in (IEnumerable <IModifiableEntity>)field !) { ((ModifiableEntity)item).SetParentEntity(this); } } } }