コード例 #1
0
ファイル: VariantPrimitive.cs プロジェクト: grobgl/protean
        public static T Parse <T>(string value)
        {
            var type = typeof(T);

            if (type == typeof(TimeSpan))
            {
                return((T)Convert.ChangeType(VariantBase.ParseTime(value), type));
            }
            else
            {
                if (type == typeof(float))
                {
                    return((T)Convert.ChangeType(VariantBase.ParseSingle(value), type));
                }
                else if (type == typeof(double))
                {
                    return((T)Convert.ChangeType(VariantBase.ParseDouble(value), type));
                }
                else if (type == typeof(bool))
                {
                    return((T)Convert.ChangeType(VariantBase.ParseBoolean(value), type));
                }
                else if (type == typeof(DateTime))
                {
                    return((T)Convert.ChangeType(VariantBase.ParseDateTime(value), type));
                }
                else
                {
                    return((T)Convert.ChangeType(value, type));
                }
            }
        }
コード例 #2
0
        public static T Parse <T>(string value)
        {
            if (typeof(T) == typeof(TimeSpan))
            {
                return((T)Convert.ChangeType(VariantBase.ParseTime(value), typeof(T)));
            }
            else
            {
                TypeCode typeCode = System.Type.GetTypeCode(typeof(T));
                switch (typeCode)
                {
                case TypeCode.Single:
                    return((T)Convert.ChangeType(VariantBase.ParseSingle(value), typeof(T)));

                case TypeCode.Double:
                    return((T)Convert.ChangeType(VariantBase.ParseDouble(value), typeof(T)));

                case TypeCode.Boolean:
                    return((T)Convert.ChangeType(VariantBase.ParseBoolean(value), typeof(T)));

                case TypeCode.DateTime:
                    return((T)Convert.ChangeType(VariantBase.ParseDateTime(value), typeof(T)));

                default:
                    return((T)Convert.ChangeType(value, typeof(T)));
                }
            }
        }