Exemple #1
0
        /// <summary>
        /// Gets the property differences between two objects of the same type.
        /// </summary>
        /// <param name="baseline">
        /// The baseline object.
        /// </param>
        /// <param name="comparison">
        /// The comparison object.
        /// </param>
        /// <param name="propertiesToCompare">
        /// The properties to compare.
        /// </param>
        /// <typeparam name="TItem">
        /// The type of item to compare.
        /// </typeparam>
        /// <returns>
        /// A collection of <see cref="PropertyComparisonResult"/> items containing the non-equivalent property values of the two
        /// items.
        /// </returns>
        public static IEnumerable <PropertyComparisonResult> GetDifferences <TItem>(
            this TItem baseline,
            TItem comparison,
            params string[] propertiesToCompare)
        {
            if (Evaluate.IsNull(baseline))
            {
                throw new ArgumentNullException(nameof(baseline));
            }

            if (Evaluate.IsNull(comparison))
            {
                throw new ArgumentNullException(nameof(comparison));
            }

            if (Evaluate.IsNull(propertiesToCompare))
            {
                throw new ArgumentNullException(nameof(propertiesToCompare));
            }

            var allProperties = GetAllProperties <TItem>(propertiesToCompare);

            var comparisonList = from p in allProperties
                                 select new
            {
                p.Name,
                OriginalValue = p.GetValue(baseline),
                NewValue      = p.GetValue(comparison)
            };

            return(from c in comparisonList
                   where !Evaluate.RecursiveEquals(c.OriginalValue, c.NewValue)
                   select new PropertyComparisonResult(c.Name, c.OriginalValue, c.NewValue));
        }
Exemple #2
0
        /// <summary>
        /// Gets property names and values for the specified item, replacing any non-serializable items with their string
        /// representations.
        /// </summary>
        /// <param name="item">
        /// The item to retrieve the properties of.
        /// </param>
        /// <param name="propertiesToInclude">
        /// The properties to include.
        /// </param>
        /// <returns>
        /// A dictionary of name value pairs joined as <see cref="string"/>, ordered by the property name.
        /// </returns>
        private static Dictionary <string, object> ToSerializableDictionary(
            this object item,
            params string[] propertiesToInclude)
        {
            if (Evaluate.IsNull(item))
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (propertiesToInclude == null)
            {
                throw new ArgumentNullException(nameof(propertiesToInclude));
            }

            return(item.ToPropertyDictionary(propertiesToInclude).ToDictionary(pair => pair.Key, GetSerializableValue));
        }
Exemple #3
0
        /// <summary>
        /// The get property value.
        /// </summary>
        /// <param name="entity">
        /// The entity.
        /// </param>
        /// <param name="propertyName">
        /// The property name.
        /// </param>
        /// <typeparam name="T">
        /// The type of entity to get the property value for.
        /// </typeparam>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="propertyName"/> or <paramref name="entity"/> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="propertyName"/> specifies a property that does not exist in <typeparamref name="T"/>.
        /// </exception>
        public static object GetPropertyValue <T>(this T entity, string propertyName)
        {
            if (string.IsNullOrWhiteSpace(propertyName))
            {
                throw new ArgumentNullException(nameof(propertyName));
            }

            if (Evaluate.IsNull(entity))
            {
                throw new ArgumentNullException(nameof(entity));
            }

            var info = typeof(T).GetProperty(propertyName);

            if (info == null)
            {
                throw new ArgumentException(
                          string.Format(CultureInfo.CurrentCulture, ValidationMessages.TypeDoesNotContainProperty, typeof(T).Name, propertyName),
                          nameof(propertyName));
            }

            return(info.GetPropertyValue(entity));
        }