ExtensionMethod.NET Home of 817 C#, Visual Basic, F# and Javascript extension methods

To<> Convert

To<> Convert

Source

/// <summary>
/// converts one type to another
/// Example:
/// var age = "28";
/// var intAge = age.To<int>();
/// var doubleAge = intAge.To<double>();
/// var decimalAge = doubleAge.To<decimal>();
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static T To<T>(this IConvertible value)
{
    try
    {
       Type t = typeof(T);
       Type u = Nullable.GetUnderlyingType(t);

        if (u != null)
        {
            if (value == null || value.Equals(""))
                return default(T);

            return (T)Convert.ChangeType(value, u);
        }
        else
        {
            if (value == null || value.Equals(""))
                return default(T);

            return (T)Convert.ChangeType(value, t);
        }
    }
    
    catch
    {
        return default(T);
    }
}

public static T To<T>(this IConvertible value, IConvertible ifError)
{
    try
    {
        Type t = typeof(T);
        Type u = Nullable.GetUnderlyingType(t);

        if (u != null)
        {
            if (value == null || value.Equals(""))
                return (T) ifError;

            return (T)Convert.ChangeType(value, u);
        }
        else
        {
            if (value == null || value.Equals(""))
                return (T)(ifError.To<T>());

            return (T)Convert.ChangeType(value, t);
        }
    }
    catch
    {

        return (T) ifError;
    }
    
}

Example

var intAge = age.To<int>();

var para = age.To<DateTime>();

Author: Majid Safari

Submitted on: 14 aug 2015

Language: C#

Type: C#

Views: 1378