您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
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)); }
关于C#中怎么自定义类型转换函数问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。