Exemple #1
0
        private static void reflectionAppend(object obj, IReflect type, HashCodeBuilder builder, bool useTransients, IEnumerable <string> excludedFields)
        {
            if (REGISTRY.Value.Contains(new IDKey(obj)))
            {
                return;
            }

            try
            {
                REGISTRY.Value.Add(new IDKey(obj));
                var values = type
                             .GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                             .Where(field => excludedFields == null || !excludedFields.Contains(field.Name))
                             .Where(field => useTransients || !field.IsNotSerialized)
                             .Select(field => field.GetValue(obj));

                foreach (var value in values)
                {
                    builder.append(value);
                }
            }

            finally
            {
                REGISTRY.Value.Remove(new IDKey(obj));
            }
        }
Exemple #2
0
        public static int reflectionHashCode(object obj, bool useTransients, IEnumerable <string> excludedFields)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj", "The object to build a hash code for must not be null");
            }

            var builder = new HashCodeBuilder(17, 37);

            for (var type = obj.GetType(); type != null && type != typeof(object); type = type.BaseType)
            {
                reflectionAppend(obj, type, builder, useTransients, excludedFields);
            }

            return(builder.toHashCode());
        }