public AuditConfiguration FormatType <T>(Func <T, object> formatter)
        {
#if !NETSTANDARD1_3
            Func <object, object> func = o =>
            {
                T obj;
                try
                {
                    obj = (T)o;
                }
                catch (Exception e)
                {
                    // Return initial object in case the cast doesn't work (such as null object)
                    return(o);
                }

                return(formatter(obj));
            };
#else
            Func <object, object> func = o => formatter((T)o);
#endif
            EntityValueFormatters.Add((x, s, v) => v != null && v.GetType() == typeof(T) ? func : null);

            return(this);
        }
        public AuditConfiguration FormatType <T>(Func <T, object> formatter)
        {
            Func <object, object> func = o => formatter((T)o);

            EntityValueFormatters.Add((x, s, v) => v != null && v.GetType() == typeof(T) ? func : null);

            return(this);
        }
Beispiel #3
0
        /// <summary>
        ///     Formats value for selected properties from entities of 'T' type or entities which the type
        ///     derive from 'T'.
        /// </summary>
        /// <typeparam name="T">Generic type to format selected properties.</typeparam>
        /// <param name="propertySelector">The property selector.</param>
        /// <param name="formatter">The formatter to use to format value.</param>
        /// <returns>An AuditConfiguration.</returns>
        public AuditConfiguration Format <T>(Expression <Func <T, object> > propertySelector, Func <object, object> formatter) where T : class
        {
            var propertyNames = propertySelector.GetPropertyOrFieldAccessors().Select(x => x.ToString()).ToList();

            foreach (var accessor in propertyNames)
            {
                EntityValueFormatters.Add((x, s) => x is T && s == accessor ? formatter : null);
            }

            return(this);
        }