Example #1
0
        public void HandleWhenChanged <TProperty>(ChangedEventHandler <TProperty> propertyChangedHandler, int propertyPathPos, params string[] propertyPath)
        {
            int    pathCount = propertyPath.Length - propertyPathPos;
            string currentpathPropertyName = propertyPath[propertyPathPos];

            if (pathCount > 1)
            {
                var  propertyInfo         = this.GetType().GetProperty(currentpathPropertyName);
                var  propertyType         = propertyInfo.PropertyType;
                Type iNotifyingObjectType = typeof(INotifyingObject);
                //var notifyingObjectType = typeof(NotifyingObject<>);
                if (iNotifyingObjectType.IsAssignableFrom(propertyType))
                {
                    INotifyingObject childNotifyingObject = (INotifyingObject)propertyInfo.GetValue(this, null);
                    if (childNotifyingObject != null)
                    {
                        childNotifyingObject.HandleWhenChanged(propertyChangedHandler, propertyPathPos + 1, propertyPath);
                    }
                    else
                    {
                        PropertyEventsPath currentPropertyEventsPath;
                        if (_propertiesEvents == null)
                        {
                            _propertiesEvents         = new Dictionary <string, PropertyEventsPath>();
                            currentPropertyEventsPath = new PropertyEventsPath();
                            _propertiesEvents.Add(currentpathPropertyName, currentPropertyEventsPath);
                        }
                        else
                        {
                            currentPropertyEventsPath = _propertiesEvents.GetValueOrAddIfNotExists(currentpathPropertyName, () => new PropertyEventsPath());
                        }
                        for (int pathPos = propertyPathPos, i = 1; i < pathCount; pathPos++, i++)
                        {
                            PropertyEventsPath child;
                            if (currentPropertyEventsPath.PropertiesEvents == null)
                            {
                                currentPropertyEventsPath.PropertiesEvents = new Dictionary <string, PropertyEventsPath>();
                                child = new PropertyEventsPath();
                                currentPropertyEventsPath.PropertiesEvents.Add(propertyPath[pathPos], child);
                            }
                            else
                            {
                                child = currentPropertyEventsPath.PropertiesEvents.GetValueOrAddIfNotExists(propertyPath[pathPos], () => new PropertyEventsPath());
                            }
                            currentPropertyEventsPath = child;
                        }
                        if (currentPropertyEventsPath.Events == null)
                        {
                            currentPropertyEventsPath.Events = new List <Delegate>();
                        }
                        currentPropertyEventsPath.Events.Add(propertyChangedHandler);
                    }
                }
                else
                {
                    throw new InvalidOperationException(string.Format(
                                                            "You can't specify a property that has a parent type which is not derived from NotifyingObject<{0}>. "
                                                            + "Details: The type \"{0}\" is not derived from NotifyingObject<{0}>", propertyType.FullName));
                }
            }
            else
            {
                PropertyEventsPath currentPropertyEventsPath;
                if (_propertiesEvents == null)
                {
                    _propertiesEvents                = new Dictionary <string, PropertyEventsPath>();
                    currentPropertyEventsPath        = new PropertyEventsPath();
                    currentPropertyEventsPath.Events = new List <Delegate>();
                    _propertiesEvents.Add(currentpathPropertyName, currentPropertyEventsPath);
                }
                else
                {
                    currentPropertyEventsPath = _propertiesEvents.GetValueOrAddIfNotExists(currentpathPropertyName, () => new PropertyEventsPath());
                    if (currentPropertyEventsPath.Events == null)
                    {
                        currentPropertyEventsPath.Events = new List <Delegate>();
                    }
                }
                currentPropertyEventsPath.Events.Add(propertyChangedHandler);
            }
        }