Ejemplo n.º 1
0
        /// <summary>
        /// Replaces a wrapped reference list, detaching contained items if needed, and confiures the wrapper to wrap a
        /// replacement list.
        /// </summary>
        /// <param name='wrapperToOverwrite'>
        /// The wrapper instance to overwrite.
        /// </param>
        /// <param name='replacement'>
        /// The replacement source list.
        /// </param>
        /// <param name='referenceProperty'>
        /// An expression indicating a property upon the contained type.
        /// </param>
        /// <param name='referenceItem'>
        /// The reference item to be stored in the <paramref name="referenceProperty"/>.
        /// </param>
        /// <typeparam name='T'>
        /// The type of item contained within the list.
        /// </typeparam>
        /// <remarks>
        /// <para>
        /// See the documentation of this type for detailled information on how this works and what it does.
        /// </para>
        /// </remarks>
        public static void Replace <T>(ref IList <T> wrapperToOverwrite,
                                       IList <T> replacement,
                                       Expression <Func <T, object> > referenceProperty,
                                       object referenceItem) where T : class
        {
            if (replacement == null)
            {
                throw new ArgumentNullException("replacement");
            }

            EventBoundListWrapper <T> typedList = wrapperToOverwrite as EventBoundListWrapper <T>;

            if (typedList != null)
            {
                typedList.DetachAll();
            }

            PropertyInfo propInfo = Reflect.Property(referenceProperty);

            foreach (T item in replacement)
            {
                propInfo.SetValue(item, referenceItem, null);
            }

            wrapperToOverwrite = GetOrInit(replacement, referenceProperty, referenceItem);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets a wrapped reference list, initialising it if required.
        /// </summary>
        /// <returns>
        /// A generic IList, with added addition and removal handlers.
        /// </returns>
        /// <param name='wrapper'>
        /// The list wrapper instance to use.
        /// </param>
        /// <param name='original'>
        /// The original/source list.
        /// </param>
        /// <param name='referenceProperty'>
        /// An expression indicating a property upon the contained type.
        /// </param>
        /// <param name='referenceItem'>
        /// The reference item to be stored in the <paramref name="referenceProperty"/>.
        /// </param>
        /// <typeparam name='T'>
        /// The type of item contained within the list.
        /// </typeparam>
        /// <remarks>
        /// <para>
        /// See the documentation of this type for detailled information on how this works and what it does.
        /// </para>
        /// </remarks>
        public static IList <T> GetOrInit <T>(ref IList <T> wrapper,
                                              ref IList <T> original,
                                              Expression <Func <T, object> > referenceProperty,
                                              object referenceItem) where T : class
        {
            EventBoundListWrapper <T> typedList = wrapper as EventBoundListWrapper <T>;

            if (typedList == null || !typedList.IsWrapping(original))
            {
                original = original ?? new List <T>();
                wrapper  = GetOrInit(original, referenceProperty, referenceItem);
            }

            return(wrapper);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initialises and returns a new list wrapper, wrapping the given <paramref name="original"/> collection.
        /// </summary>
        /// <returns>
        /// A generic IList, with added addition and removal handlers.
        /// </returns>
        /// <param name='original'>
        /// The original/source list.
        /// </param>
        /// <param name='referenceProperty'>
        /// An expression indicating a property upon the contained type.
        /// </param>
        /// <param name='referenceItem'>
        /// The reference item to be stored in the <paramref name="referenceProperty"/>.
        /// </param>
        /// <typeparam name='T'>
        /// The type of item contained within the list.
        /// </typeparam>
        private static EventBoundListWrapper <T> GetOrInit <T>(IList <T> original,
                                                               Expression <Func <T, object> > referenceProperty,
                                                               object referenceItem) where T : class
        {
            if (original == null)
            {
                throw new ArgumentNullException("original");
            }

            EventBoundListWrapper <T> output = original as EventBoundListWrapper <T>;

            if (output == null)
            {
                PropertyInfo propInfo = Reflect.Property(referenceProperty);

                output = original.WrapWithBeforeActions((list, item) => BeforeAdd(list, item, propInfo, referenceItem),
                                                        (list, item) => BeforeRemove(list, item, propInfo));
            }

            return(output);
        }