这里将介绍常用自定义C#类型转换函数,大家经常碰到类弄转换,但都不知道哪些系统函数才可以转换。希望本文能对大家有所帮助。
- /// <summary>
- /// 将字符型类型转换为整型值
- /// </summary>
- /// <param name="objValue">字符型</param>
- /// <param name="defaultValue">无法转换时的默认值</param>
- /// <returns>整型值</returns>
- public static int IntParse(string objValue, int defaultValue)
- {
- int returnValue = defaultValue;
- if (!string.IsNullOrEmpty(objValue))
- {
- try
- {
- returnValue = int.Parse(objValue);
- }
- catch
- {
- returnValue = defaultValue;
- }
- }
- return returnValue;
- }
- /// <summary>
- /// 将对象类型转换为整型值
- /// </summary>
- /// <param name="objValue">对象类型</param>
- /// <param name="defaultValue">无法转换时的默认值</param>
- /// <returns>整型值</returns>
- public static int IntParse(object objValue, int defaultValue)
- {
- int returnValue = defaultValue;
- if (objValue != null && objValue != DBNull.Value)
- {
- try
- {
- returnValue = int.Parse(objValue.ToString());
- }
- catch
- {
- returnValue = defaultValue;
- }
- }
- //返回值
- return returnValue;
- }
- /// <summary>
- /// 将对象类型转换为整型值
- /// </summary>
- /// <param name="objValue">对象类型</param>
- /// <returns>整型值</returns>
- public static int IntParse(object objValue)
- {
- return IntParse(objValue, 0);
- }
- /// <summary>
- /// 将对象类型转换为日期值
- /// </summary>
- /// <param name="objValue">对象类型</param>
- /// <param name="defaultValue">无法转换时的默认值</param>
- /// <returns>日期值</returns>
- public static DateTime DateTimeParse(object objValue, DateTime defaultValue)
- {
- DateTime returnValue = defaultValue;
- if (objValue != null && objValue != DBNull.Value)
- {
- try
- {
- returnValue = DateTime.Parse(objValue.ToString());
- }
- catch
- {
- returnValue = defaultValue;
- }
- }
- //返回值
- return returnValue;
- }
- /// <summary>
- /// 将对象类型转换为日期值
- /// </summary>
- /// <param name="objValue">对象类型</param>
- /// <returns>日期值</returns>
- public static DateTime DateTimeParse(object objValue)
- {
- return DateTimeParse(objValue, DateTime.MinValue);
- }
- /// <summary>
- /// 将对象类型转换为字符型
- /// </summary>
- /// <param name="objValue">对象类型</param>
- /// <param name="defaultValue">无法转换时的默认值</param>
- /// <returns>字符型</returns>
- public static string StringParse(object objValue, string defaultValue)
- {
- string returnValue = defaultValue;
- if (objValue != null && objValue != DBNull.Value)
- {
- try
- {
- returnValue = objValue.ToString();
- }
- catch
- {
- returnValue = defaultValue; ;
- }
- }
- //返回值
- return returnValue;
- }
- /// <summary>
- /// 将对象类型转换为字符型
- /// </summary>
- /// <param name="objValue">对象类型</param>
- /// <returns>字符型</returns>
- public static string StringParse(object objValue)
- {
- return StringParse(objValue, string.Empty);
- }
- /// <summary>
- /// 将对象类型转换为GUID
- /// </summary>
- /// <param name="objValue">对象类型</param>
- /// <param name="defaultValue">无法转换时的默认值</param>
- /// <returns>GUID</returns>
- public static Guid GuidParse(object objValue, Guid defaultValue)
- {
- Guid returnValue = defaultValue;
- if (objValue != null && objValue != DBNull.Value)
- {
- try
- {
- returnValue = new Guid(objValue.ToString());
- }
- catch
- {
- returnValue = defaultValue; ;
- }
- }
- //返回值
- return returnValue;
- }
- /// <summary>
- /// 将对象类型转换为GUID
- /// </summary>
- /// <param name="objValue">对象类型</param>
- /// <returns>GUID</returns>
- public static Guid GuidParse(object objValue)
- {
- return GuidParse(objValue, Guid.Empty);
- }
- /// <summary>
- /// C#类型转换函数
- /// </summary>
- /// <typeparam name="T">目标类型值</typeparam>
- /// <param name="objValue">对象类型</param>
- /// <param name="defaultValue">无法转换时的默认值</param>
- /// <returns>目标类型值</returns>
- public static T Parse<T>(object objValue, T defaultValue)
- {
- T returnValue = defaultValue;
- if (objValue != null && objValue != DBNull.Value)
- {
- try
- {
- returnValue = (T)objValue;
- }
- catch
- {
- returnValue = defaultValue;
- }
- }
- //返回值
- return returnValue;
- }
- /// <summary>
- /// C#类型转换函数
- /// </summary>
- /// <typeparam name="T">目标类型值</typeparam>
- /// <param name="objValue">对象类型</param>
- /// <returns>目标类型值</returns>
- public static T Parse<T>(object objValue)
- {
- return Parse<T>(objValue, default(T));
- }
【编辑推荐】