Beispiel #1
0
        public void Subscribe(INotifyPropertyChanged n)
        {
            if (_subscribed)
            {
                throw new InvalidOperationException("Notifier subscribed twice");
            }
            _subscribed = true;

            foreach (var method in n.GetType().GetMethods())
            {
                foreach (var triggedOn in method.GetCustomAttributes().OfType <TriggedOn>())
                {
                    switch (method.GetParameters().Length)
                    {
                    case 0:
                        n.SubscribeNotifier((s, args) => method.Invoke(n, null), triggedOn.Pathes);
                        break;

                    case 1:
                        n.SubscribeNotifier((s, args) => method.Invoke(n, new object[] { args }), triggedOn.Pathes);
                        break;

                    case 2:
                        n.SubscribeNotifier((s, args) => method.Invoke(n, new object[] { s, args }), triggedOn.Pathes);
                        break;
                    }
                }
            }

            foreach (var property in n.GetType().GetProperties())
            {
                var name = property.Name;
                foreach (var triggedOn in property.GetCustomAttributes().OfType <TriggedOn>())
                {
                    if (typeof(ITriggable).IsAssignableFrom(property.PropertyType))
                    {
                        n.SubscribeNotifier((s, a) =>
                        {
                            if (IsSet(property))
                            {
                                Get <ITriggable>(n, null, name).OnTrigged();
                            }
                            else
                            {
                                //OnPropertyChanged(name);
                                OnPropertyChanged(new NotifierPropertyChangedEventArgs(property.Name, null, null));
                            }
                            //property.GetMethod.Invoke(n, null);
                        }, triggedOn.Pathes);
                    }
                    else
                    {
                        n.SubscribeNotifier((s, a) =>
                        {
                            if (IsSet(property))
                            {
                                Update(property);
                            }
                            else
                            {
                                //OnPropertyChanged(name);
                                OnPropertyChanged(new NotifierPropertyChangedEventArgs(property.Name, null, null));
                            }
                            //property.GetMethod.Invoke(n, null);
                        }, triggedOn.Pathes);
                    }
                }
            }
        }