예제 #1
0
        private IHashCodeBuilder GetBuilder(Type instanceType)
        {
            if (!_builderCache.ContainsKey(instanceType))
            {
                IEnumerable<PropertyInfo> targetProperties = HashCodeParameterAttribute.GetDemarcatedProperties(instanceType);
                var builder = new ReflectionHashCodeBuilder(targetProperties, this);
                this.Register(instanceType, builder);
            }

            return _builderCache[instanceType];
        }
예제 #2
0
        public static string[] GetParameters(object instance, IEnumerable <PropertyInfo> properties, IHashCodeBuilderFactory builderFactory)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            var parameterValues = new List <string>();

            foreach (PropertyInfo property in properties)
            {
                object propValue = property.GetValue(instance, null);
                if (propValue == null)
                {
                    continue;
                }

                Type propertyType = property.PropertyType;
                if (propValue is System.Collections.ICollection)
                {
                    parameterValues.AddRange(GetParametersFromCollection(propValue as System.Collections.ICollection, builderFactory));
                    continue;
                }

                if (Helpers.IsPrimitiveType(propertyType))
                {
                    parameterValues.Add(propValue.ToString());
                }
                else
                {
                    var hashCodeBuilder     = builderFactory.CreateInstance(propertyType) as ReflectionHashCodeBuilder;
                    var propValueProperties = hashCodeBuilder != null ?
                                              hashCodeBuilder.TargetProperties : HashCodeParameterAttribute.GetDemarcatedProperties(propertyType);

                    parameterValues.AddRange(GetParameters(propValue, propValueProperties, builderFactory));
                }
            }

            return(parameterValues
                   .Where(val => !string.IsNullOrEmpty(val))
                   .ToArray());
        }