コード例 #1
0
ファイル: ValueParser.cs プロジェクト: gzlive/behaven
        /// <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));
        }
コード例 #2
0
ファイル: Grid.cs プロジェクト: gzlive/behaven
        /// <summary>
        /// Converts the block into an object.
        /// </summary>
        /// <param name="type">The type of object to convert to.</param>
        /// <returns>The new object.</returns>
        public object ConvertTo(Type type)
        {
            Type itemType = TypeExtensions.GetCollectionItemType(type);

            var list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(itemType));

            for (int rowIndex = 0; rowIndex < this.RowCount; rowIndex++)
            {
                object item = CreateItem(itemType, rowIndex);

                list.Add(item);
            }

            return(list);
        }
コード例 #3
0
 /// <summary>
 /// Determines if this type handles the specified type.
 /// </summary>
 /// <param name="type">The type that can potentially be handled by this block type.</param>
 /// <returns>
 /// true if this type handles the specified type
 /// </returns>
 public override bool HandlesType(Type type)
 {
     return(TypeExtensions.GetCollectionItemType(type) == null && type != typeof(StringBuilder));
 }
コード例 #4
0
 public override bool HandlesType(Type type)
 {
     return(type != typeof(string) && TypeExtensions.GetCollectionItemType(type) != null);
 }