Com.PhilChuang.Utils.MvvmNotificationChainer.NotificationChainPropertyAttribute.CallProperties C# (CSharp) Method

CallProperties() public static method

Looks for every property decorated with the NotificationChainPropertyAttribute and calls the getter, which should initialize any NotificationChains.
public static CallProperties ( object obj ) : void
obj object
return void
        public static void CallProperties(object obj)
        {
            obj.ThrowIfNull ("obj");

            var objType = obj.GetType ();

            foreach (var prop in objType.GetRuntimeProperties())
            {
                if (!prop.GetCustomAttributes (typeof (NotificationChainPropertyAttribute), true).Any ()) continue;
                var propGetter = prop.GetMethod;
                if (propGetter == null) continue;
                if (propGetter.GetParameters ().Any ())
                    throw new InvalidOperationException ("NotificationChainPropertyAttribute cannot be applied to property {0}.{1} because it has parameters."
                                                             .FormatWith (prop.DeclaringType.FullName, prop.Name));
                var value = propGetter.Invoke (!propGetter.IsStatic ? obj : null, null);
            }

            // TODO think about this more
            //foreach (var method in
            //    objType.GetMethods (BindingFlags.Public | BindingFlags.Instance)
            //           .Union (objType.GetMethods (BindingFlags.NonPublic | BindingFlags.Instance))
            //           .Union (objType.GetMethods (BindingFlags.Public | BindingFlags.Static))
            //           .Union (objType.GetMethods (BindingFlags.NonPublic | BindingFlags.Static)))
            //{
            //    if (!method.GetCustomAttributes (typeof (NotificationChainPropertyAttribute), true).Any ()) continue;
            //    if (method.GetParameters ().Any ())
            //        throw new InvalidOperationException ("NotificationChainPropertyAttribute cannot be applied to method {0}.{1} because it has parameters."
            //                                                 .FormatWith (method.DeclaringType.FullName, method.Name));
            //    var value = method.Invoke (!method.IsStatic ? obj : null, null);
            //}
        }

Usage Example

        /// <summary>
        /// Begins observing the given notifying object.
        /// </summary>
        /// <param name="notifyingObject"></param>
        /// <param name="addEventAction"></param>
        /// <param name="removeEventAction"></param>
        public void Observe(
            Object notifyingObject,
            Action <PropertyChangedEventHandler> addEventAction,
            Action <PropertyChangedEventHandler> removeEventAction)
        {
            notifyingObject.ThrowIfNull("notifyingObject");
            addEventAction.ThrowIfNull("addEventAction");
            removeEventAction.ThrowIfNull("removeEventAction");

            if (IsDisposed)
            {
                return;
            }

            if (myObservedObjects.ContainsKey(notifyingObject))
            {
                return;
            }

            myObservedObjects[notifyingObject] = () => removeEventAction(myPropertyChangedEventHandler);

            NotificationChainPropertyAttribute.CallProperties(notifyingObject);

            addEventAction(myPropertyChangedEventHandler);
        }
All Usage Examples Of Com.PhilChuang.Utils.MvvmNotificationChainer.NotificationChainPropertyAttribute::CallProperties
NotificationChainPropertyAttribute