Convert string to any type that implements TryParse method

String extension method for converting to other value types

  • Share

In many cases serialized data is returned in a form of string although the actual type of data is different. The problem begings when you want to use data in it's original type becaus direct cast is not allowed in C# as it is in VB.net.

This is why you have to use TryParse and safely parse the value to it's original type. The prerequisite is that you know the actual data type in order to cast the value to that type.

The following is extension method for string type which allows safe casting from string to any type that implements IConvertible which means that it has implementd TryParse method

public static class ExtensionMethods
    {
        /// <summary>
        /// Converting the string value to T type
        /// </summary>
        /// <typeparam name="T">Type to convert string value to</typeparam>
        /// <param name="value">String value to be converted to type T</param>
        /// <returns>Returns converted string value to type T</returns>
        public static T ConvertTo<T>(this string value) where T : IConvertible
        {
            var typeCode = default(T).GetTypeCode();
            switch (typeCode)
            {
                case TypeCode.Boolean:
                    Boolean boolVal = default(Boolean);
                    Boolean.TryParse(value, out boolVal);
                    return (T)Convert.ChangeType(boolVal, typeCode);

                case TypeCode.Byte:
                    Byte ByteVal = default(Byte);
                    Byte.TryParse(value, out ByteVal);
                    return (T)Convert.ChangeType(ByteVal, typeCode);

                case TypeCode.Char:
                    Char CharVal = default(Char);
                    Char.TryParse(value, out CharVal);
                    return (T)Convert.ChangeType(CharVal, typeCode);

                case TypeCode.DateTime:
                    DateTime DatetTimeVal = default(DateTime);
                    DateTime.TryParse(value, out DatetTimeVal);
                    return (T)Convert.ChangeType(DatetTimeVal, typeCode);

                case TypeCode.Decimal:
                    Decimal DecimalVal = default(Decimal);
                    Decimal.TryParse(value, out DecimalVal);
                    return (T)Convert.ChangeType(DecimalVal, typeCode);

                case TypeCode.Double:
                    Double DoubleVal = default(Double);
                    Double.TryParse(value, out DoubleVal);
                    return (T)Convert.ChangeType(DoubleVal, typeCode);

                case TypeCode.SByte:
                    SByte SByteVal = default(SByte);
                    SByte.TryParse(value, out SByteVal);
                    return (T)Convert.ChangeType(SByteVal, typeCode);

                case TypeCode.Single:
                    Single SingleVal = default(Single);
                    Single.TryParse(value, out SingleVal);
                    return (T)Convert.ChangeType(SingleVal, typeCode);

                case TypeCode.UInt16:
                    UInt16 UInt16Val = default(UInt16);
                    UInt16.TryParse(value, out UInt16Val);
                    return (T)Convert.ChangeType(UInt16Val, typeCode);

                case TypeCode.UInt32:
                    UInt32 UInt32Val = default(UInt16);
                    UInt32.TryParse(value, out UInt32Val);
                    return (T)Convert.ChangeType(UInt32Val, typeCode);

                case TypeCode.UInt64:
                    UInt64 UInt64Val = default(UInt16);
                    UInt64.TryParse(value, out UInt64Val);
                    return (T)Convert.ChangeType(UInt64Val, typeCode);

                case TypeCode.Int16:
                    Int16 int16Val = default(Int16);
                    Int16.TryParse(value, out int16Val);
                    return (T)Convert.ChangeType(int16Val, typeCode);

                case TypeCode.Int32:
                    Int32 int32Val = 0;
                    Int32.TryParse(value, out int32Val);
                    return (T)Convert.ChangeType(int32Val, typeCode);

                case TypeCode.Int64:
                    Int64 Int64Val = 0;
                    Int64.TryParse(value, out Int64Val);
                    return (T)Convert.ChangeType(Int64Val, typeCode);
            }

            return default(T);
        }
    }
    

The long switch/case does not look so nice but I havent found the shorter way to handle all the type, but wrapped as an extension method can be used only with single method call.

References

  • Share

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.

Comments for this article

comments powered by Disqus