示例#1
0
        private bool CheckProperty(object actual, int i, Type type)
        {
            bool passed = true;

            string label        = this.GetLabel(i);
            string propertyName = NameComparer.NormalizeName(label);

            PropertyInfo pi = ValueGetter.GetPropertyInfo(type, propertyName);

            if (pi != null)
            {
                object actualValue   = pi.GetValue(actual, null);
                object expectedValue = ValueParser.ParseValue(this.GetValue(i), pi.PropertyType);

                if (!object.Equals(actualValue, expectedValue))
                {
                    this.values[i] = string.Format("{0} (was {1})", expectedValue, actualValue);
                    passed         = false;
                }
            }
            else
            {
                this.values[i] = string.Format("{0} (unknown)", this.GetValue(i));
                passed         = false;
            }

            return(passed);
        }
示例#2
0
        /// <summary>
        /// Parses the value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="type">The type.</param>
        /// <returns>The actual value.</returns>
        public static object ParseValue(string value, Type type)
        {
            if (value == null)
            {
                if (type.IsValueType)
                {
                    return(Activator.CreateInstance(type));
                }

                return(null);
            }

            if (TypeExtensions.IsNullable(type))
            {
                if (value == "null")
                {
                    return(null);
                }

                type = Nullable.GetUnderlyingType(type);
            }

            if (type.IsEnum)
            {
                return(Enum.Parse(type, NameComparer.NormalizeName(value), true));
            }

            if (type == typeof(int))
            {
                return(Int32Parser.ParseInt32(value));
            }

            if (type == typeof(DateTime))
            {
                return(DateTimeParser.ParseDateTime(value));
            }

            Type itemType = TypeExtensions.GetCollectionItemType(type);

            if (itemType != null)
            {
                IList list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(itemType));

                string[] splits = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string split in splits)
                {
                    list.Add(ParseValue(split.Trim(), itemType));
                }

                return(list);
            }

            // TODO: Figure out how to configure which format provider to use!
            return(Convert.ChangeType(value, type, CultureInfo.InvariantCulture));
        }
示例#3
0
        /// <summary>
        /// Gets the value setter.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="name">The name.</param>
        /// <returns>A <see cref="ValueSetter" /> that can set the named value.</returns>
        public static ValueSetter GetValueSetter(object target, string name)
        {
            string normalizedName = NameComparer.NormalizeName(name);

            return(PropertyDescriptorValueSetter.GetPropertyDescriptorValueSetter(target, normalizedName)
                   ?? MethodInfoValueSetter.GetMethodInfoValueSetter(target, normalizedName)
                   ?? PropertyInfoValueSetter.GetPropertyInfoValueSetter(target, normalizedName)
                   ?? FieldInfoValueSetter.GetFieldInfoValueSetter(target, normalizedName)
                   ?? (ValueSetter) new NullValueSetter());
        }
示例#4
0
        /// <summary>
        /// Compares the values.
        /// </summary>
        /// <param name="actual">The actual.</param>
        /// <param name="expected">The expected.</param>
        /// <param name="type">The type.</param>
        /// <returns>True or false.</returns>
        public static bool CompareValues(string actual, string expected, Type type)
        {
            if (type.IsEnum || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>) && type.GetGenericArguments()[0].IsEnum))
            {
                actual   = NameComparer.NormalizeName(actual);
                expected = NameComparer.NormalizeName(expected);
            }
            else if (type == typeof(DateTime) || type == typeof(DateTime?))
            {
                expected = ValueFormatter.FormatValue(DateTimeParser.ParseDateTime(expected));
            }

            return(string.Equals(actual, expected, StringComparison.OrdinalIgnoreCase));
        }
示例#5
0
        internal static PropertyDescriptorValueSetter GetPropertyDescriptorValueSetter(object target, string normalizedName)
        {
            if (target is ICustomTypeDescriptor)
            {
                ICustomTypeDescriptor        typeDescriptor = (ICustomTypeDescriptor)target;
                PropertyDescriptorCollection properties     = typeDescriptor.GetProperties();

                foreach (PropertyDescriptor property in properties)
                {
                    string normalizedPropertyName = NameComparer.NormalizeName(property.Name);

                    if (NameComparer.NormalizedNamesAreEqual(normalizedPropertyName, normalizedName) && !property.IsReadOnly)
                    {
                        return(new PropertyDescriptorValueSetter(target, property));
                    }
                }
            }

            return(null);
        }
示例#6
0
文件: Grid.cs 项目: gzlive/behaven
        /// <summary>
        /// Gets the property info specified by the header.
        /// </summary>
        /// <param name="type">The type containing the property.</param>
        /// <param name="header">The header.</param>
        /// <returns>The property info.</returns>
        private PropertyInfo GetPropertyInfo(Type type, string header)
        {
            string propertyName = NameComparer.NormalizeName(header);

            return(ValueGetter.GetPropertyInfo(type, propertyName));
        }