【学艺不精系列】C#类型转换,待续……

/// <summary>
/// 数据类型转换,静态类
/// </summary>
public static class Parse {

    #region string
    /// <summary>转为字符串,若 obj 为 null,则返回""</summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static string ToString(object obj) {
        if (obj == null) return "";
        return obj.ToString();
    }

    /// <summary>转为字符串,若 obj 为 null,则返回null</summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static string ToStringN(object obj) {
        if (obj == null) return null;
        return obj.ToString();
    }
    #endregion

    #region int
    /// <summary>尝试转为 int 型,若转换失败则返回 0</summary>
    /// <param name="obj">参数</param>
    /// <returns></returns>
    public static int ToInt(object obj) {
        int i = 0;
        ToInt(obj, out i);
        return i;
    }

    /// <summary>尝试转为 int 型,若转换失败则结果为 0</summary>
    /// <param name="obj">参数</param>
    /// <param name="msg">转换结果</param>
    /// <returns>失败原因</returns>
    public static string ToInt(object obj, out int result) {
        if (obj == null) {
            result = 0;
            return "参数obj为null。";
        } else {
            try {
                result = int.Parse(ToString(obj));
                return "";
            } catch (Exception e) {
                result = 0;
                return e.Message;
            }
        }
    }

    /// <summary>使用 int.TryParse 方法来尝试转换 int 型,若转换失败则返回 0</summary>
    /// <param name="obj">待转换参数</param>
    /// <returns></returns>
    public static int TryInt(object obj) {
        int i = 0;
        int.TryParse(ToString(obj), out i);
        return i;
    }

    /// <summary>使用 int.TryParse 方法来尝试转换 int 类型,若转换失败则返回默认值</summary>
    /// <param name="obj">待转换参数</param>
    /// <param name="def">默认值</param>
    /// <returns></returns>
    public static int TryInt(object obj, int def) {
        if (obj == null)
            return def;
        int.TryParse(ToString(obj), out def);
        return def;
    }

    /// <summary>使用 int.TryParse 方法来尝试转为 int? 型,若 obj 为 null,则返回 null 值</summary>
    /// <param name="obj">待转换参数</param>
    /// <param name="realnull">true: 若返回 null 值仅仅是因为 obj 为 null,转换失败将返回 0;false: 若返回 null 值可能是转换失败的结果</param>
    /// <returns></returns>
    public static int? TryInt(object obj, bool realnull) {
        if (obj == null) return null;
        int i = 0;
        if (int.TryParse(ToString(obj), out i) || realnull) {
            return i;
        } else {
            return null;
        }
    }
    #endregion

    #region double
    /// <summary>尝试转为 double 型,若转换失败则返回 0</summary>
    /// <param name="obj">参数</param>
    /// <returns></returns>
    public static double ToDouble(object obj) {
        double i = 0.00;
        ToDouble(obj, out i);
        return i;
    }

    /// <summary>尝试转为 double 型,若转换失败则结果为 0</summary>
    /// <param name="obj">参数</param>
    /// <param name="msg">转换结果</param>
    /// <returns>失败原因</returns>
    public static string ToDouble(object obj, out double result) {
        if (obj == null) {
            result = 0.00;
            return "参数obj为null。";
        } else {
            try {
                result = double.Parse(ToString(obj));
                return "";
            } catch (Exception e) {
                result = 0.00;
                return e.Message;
            }
        }
    }

    /// <summary>使用 double.TryParse 方法来尝试转换 double 型,若转换失败则返回 0</summary>
    /// <param name="obj">待转换参数</param>
    /// <returns></returns>
    public static double TryDouble(object obj) {
        double i = 0.00;
        double.TryParse(ToString(obj), out i);
        return i;
    }

    /// <summary>使用 double.TryParse 方法来尝试转换 double 类型,若转换失败则返回默认值</summary>
    /// <param name="obj">待转换参数</param>
    /// <param name="def">默认值</param>
    /// <returns></returns>
    public static double TryDouble(object obj, double def) {
        if (obj == null)
            return def;
        double.TryParse(ToString(obj), out def);
        return def;
    }

    /// <summary>使用 double.TryParse 方法来尝试转为 double? 型,若 obj 为 null,则返回 null 值</summary>
    /// <param name="obj">待转换参数</param>
    /// <param name="realnull">true: 若返回 null 值仅仅是因为 obj 为 null,转换失败将返回 0;false: 若返回 null 值可能是转换失败的结果</param>
    /// <returns></returns>
    public static double? TryDouble(object obj, bool realnull) {
        if (obj == null) return null;
        double i = 0.00;
        if (double.TryParse(ToString(obj), out i) || realnull) {
            return i;
        } else {
            return null;
        }
    }
    #endregion

    #region bool
    /// <summary>尝试转为 bool 型</summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static bool ToBool(object obj) {
        bool i = false;
        ToBool(obj, out i);
        return i;
    }

    /// <summary>尝试转为 bool 型,并返回失败原因</summary>
    /// <param name="obj">待转换参数</param>
    /// <param name="result">转换结果</param>
    /// <returns>失败原因</returns>
    public static string ToBool(object obj, out bool result) {
        if (obj == null) {
            result = false;
            return "参数obj为null";
        } else {
            try {
                result = bool.Parse(obj.ToString());
                return "";
            } catch (Exception e) {
                result = false;
                return e.Message;
            }
        }
    }

    /// <summary>使用 bool.TryParse 方法来尝试转换 bool 型</summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static bool TryBool(object obj) {
        bool i = false;
        bool.TryParse(ToString(obj), out i);
        return i;
    }
    #endregion

    #region DateTime
    /// <summary>使用 DateTime.TryParse 方法来尝试转为 DateTime 型,若失败则返回 1900-01-01 00:00:00</summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static DateTime TryDateTime(object obj) {
        DateTime i = new DateTime(1900, 1, 1, 0, 0, 0);
        DateTime.TryParse(ToString(obj), out i);
        return i;
    }

    /// <summary>使用 DateTime.TryParse 方法来尝试转为 DateTime? 型,若 obj 为 null,则返回 null 值</summary>
    /// <param name="obj">待转换的参数</param>
    /// <param name="realnull">true: 若返回 null 值仅仅是因为 obj 为 null,转换失败将返回 1900-01-01 00:00:00;false: 若返回 null 值可能是转换失败的结果</param>
    /// <returns></returns>
    public static DateTime? TryDateTime(object obj, bool realnull) {
        if (obj == null) return null;
        DateTime i = new DateTime(1900, 1, 1, 0, 0, 0);
        if (DateTime.TryParse(ToString(obj), out i) || realnull) {
            return i;
        } else {
            return null;
        }
    }
    #endregion

}

posted on 2012-06-18 17:08  飞扬的尘埃  阅读(425)  评论(0编辑  收藏  举报

导航