<address id="xpjh9"><listing id="xpjh9"><meter id="xpjh9"></meter></listing></address>

<address id="xpjh9"><address id="xpjh9"><listing id="xpjh9"></listing></address></address>
<noframes id="xpjh9">
<noframes id="xpjh9">

<address id="xpjh9"><address id="xpjh9"><listing id="xpjh9"></listing></address></address>
    <form id="xpjh9"></form><address id="xpjh9"><listing id="xpjh9"><menuitem id="xpjh9"></menuitem></listing></address>

      <noframes id="xpjh9">
      VB.net 2010 視頻教程 VB.net 2010 視頻教程 VB.net 2010 視頻教程
      SQL Server 2008 視頻教程 c#入門經典教程 Visual Basic從門到精通視頻教程
      當前位置:
      首頁 > 編程開發 > C#編程 >
      • C#教程之類型轉換一種處理方式

      • 2019-07-13 21:31 來源:未知

      類型轉換一種處理方式

       

      背景#

      1. 在開發中,我們會碰到諸如String類型轉換為Int等等問題,雖然處理起來簡單,但是本著DRY(Don't Repeat Yourself )原則,還是有必要封裝處理下;
      2. 具體代碼:MasterChief
      3. Nuget:Install-Package MasterChief.DotNet4.Utilities
      4. 歡迎Star,歡迎Issues;

      實現代碼#

      
      		
      Copy
      using System; using System.ComponentModel; using System.Data; using System.Globalization; namespace MasterChief.DotNet4.Utilities.Common { /// <summary> /// 轉換幫助類 /// </summary> public static class ConvertHelper { #region Methods /// <summary> /// 轉換成布爾類型 /// </summary> /// <param name="data">需要轉換的object</param> /// <param name="defalut">默認數值</param> /// <returns>轉換返回</returns> public static bool ToBooleanOrDefault(this object data, bool defalut = false) { return data != null && bool.TryParse(data.ToString(), out var result) ? result : defalut; } /// <summary> /// 轉換成Byte類型 /// </summary> /// <param name="data">需要轉換的object</param> /// <param name="defalut">默認數值</param> /// <returns>轉換返回</returns> public static byte ToByteOrDefault(this object data, byte defalut = 0x00) { return data != null && byte.TryParse(data.ToString(), out var result) ? result : defalut; } /// <summary> /// 轉換為農歷年 /// </summary> /// <param name="date">日期</param> /// <returns>農歷年</returns> public static string ToChineseDate(this DateTime date) { var cnDate = new ChineseLunisolarCalendar(); string[] months = { string.Empty, "正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "冬月", "臘月" }; string[] days = { string.Empty, "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十" }; string[] years = { string.Empty, "甲子", "乙丑", "丙寅", "丁卯", "戊辰", "己巳", "庚午", "辛未", "壬申", "癸酉", "甲戌", "乙亥", "丙子", "丁丑", "戊寅", "己卯", "庚辰", "辛己", "壬午", "癸未", "甲申", "乙酉", "丙戌", "丁亥", "戊子", "己丑", "庚寅", "辛卯", "壬辰", "癸巳", "甲午", "乙未", "丙申", "丁酉", "戊戌", "己亥", "庚子", "辛丑", "壬寅", "癸丑", "甲辰", "乙巳", "丙午", "丁未", "戊申", "己酉", "庚戌", "辛亥", "壬子", "癸丑", "甲寅", "乙卯", "丙辰", "丁巳", "戊午", "己未", "庚申", "辛酉", "壬戌", "癸亥" }; var year = cnDate.GetYear(date); var yearCn = years[cnDate.GetSexagenaryYear(date)]; int month = cnDate.GetMonth(date), day = cnDate.GetDayOfMonth(date), leapMonth = cnDate.GetLeapMonth(year); var monthCn = months[month]; if (leapMonth > 0) { monthCn = month == leapMonth ? $"閏{months[month - 1]}" : monthCn; monthCn = month > leapMonth ? months[month - 1] : monthCn; } return $"{yearCn}{monthCn}{days[day]}"; } /// <summary> /// 將阿拉伯數字轉換中文日期數字 /// </summary> /// <param name="data">日期范圍1~31</param> /// <returns>中文日期數字</returns> public static string ToChineseDay(int data) { string result = string.Empty; if (!(data == 0 || data > 32)) { string[] days = { "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "廿十", "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十", "三十一" }; result = days[data]; } return result; } /// <summary> /// 將阿拉伯數字轉換成中文月份數字 /// <para>eg:ConvertHelper.ToChineseMonth(1)==> "一"</para> /// </summary> /// <param name="data">月份范圍1~12</param> /// <returns>中文月份數字</returns> public static string ToChineseMonth(this int data) { var result = string.Empty; if (!(data == 0 || data > 12)) { string[] months = { "〇", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" }; result = months[data]; } return result; } /// <summary> /// 轉換成日期 /// </summary> /// <param name="data">數據</param> /// <param name="defalut">默認數值</param> /// <returns>日期</returns> public static DateTime ToDateOrDefault(this object data, DateTime defalut) { return DateTime.TryParse(data.ToString(), out var result) ? result : defalut; } /// <summary> /// 日期轉化 /// </summary> /// <returns>目標日期</returns> /// <param name="data">數據.</param> public static DateTime ToDateOrDefault(this object data) { return ToDateOrDefault(data, DateTime.MinValue); } /// <summary> /// 轉換成decimal類型 /// </summary> /// <param name="data">需要轉換的object</param> /// <param name="defalut">默認數值</param> /// <returns>轉換返回</returns> public static decimal ToDecimalOrDefault(this object data, decimal defalut = 0m) { if (data != null) { var result = decimal.TryParse(data.ToString(), out var parsedecimalValue); return result ? parsedecimalValue : defalut; } return defalut; } /// <summary> /// 轉換成double類型 /// </summary> /// <param name="data">需要轉換的object</param> /// <param name="defalut">默認數值</param> /// <returns>轉換返回</returns> public static double ToDoubleOrDefault(this object data, double defalut = 0d) { if (data != null) { var result = double.TryParse(data.ToString(), out var parseIntValue); return result ? parseIntValue : defalut; } return defalut; } /// <summary> /// 轉換成Int32類型 /// </summary> /// <param name="data">需要轉換的object</param> /// <param name="defalut">默認數值</param> /// <returns>轉換返回</returns> public static int ToInt32OrDefault(this object data, int defalut = 0) { if (data != null) { var result = int.TryParse(data.ToString(), out var parseIntValue); return result ? parseIntValue : defalut; } return defalut; } /// <summary> /// 轉換成Int64類型 /// </summary> /// <param name="data">需要轉換的object</param> /// <param name="defalut">默認數值</param> /// <returns>轉換返回</returns> public static long ToInt64OrDefault(this object data, long defalut = 0) { if (data != null) { var result = long.TryParse(data.ToString(), out var parseIntValue); return result ? parseIntValue : defalut; } return defalut; } /// <summary> /// 轉換成Int類型 /// </summary> /// <param name="data">需要轉換的object</param> /// <param name="defalut">默認數值</param> /// <returns>轉換返回</returns> public static int ToIntOrDefault(this object data, int defalut = 0) { if (data != null) { var result = int.TryParse(data.ToString(), out var parseIntValue); return result ? parseIntValue : defalut; } return defalut; } /// <summary> /// 按照列名稱獲取Int值 /// </summary> /// <param name="row">DataRow</param> /// <param name="columnName">列名稱</param> /// <param name="defalut">默認數值</param> /// <returns>若列不等于NULL則返回實際值</returns> public static int ToIntOrDefault(this DataRow row, string columnName, int defalut = 0) { if (row != null) if (row.IsNull(columnName)) int.TryParse(row[columnName].ToString(), out defalut); return defalut; } /// <summary> /// 按照列索引獲取Int值 /// </summary> /// <param name="row">DataRow</param> /// <param name="columnIndex">列索引</param> /// <param name="defalut">默認數值</param> /// <returns>若列不等于NULL則返回實際值</returns> public static int ToIntOrDefault(this DataRow row, int columnIndex, int defalut = 0) { if (row != null) if (row.IsNull(columnIndex)) int.TryParse(row[columnIndex].ToString(), out defalut); return defalut; } /// <summary> /// 轉換成Int16類型 /// </summary> /// <param name="data">需要轉換的object</param> /// <param name="defalut">默認數值</param> /// <returns>轉換返回</returns> public static short ToShortOrDefault(this object data, short defalut) { if (data != null) { var result = short.TryParse(data.ToString(), out var parseIntValue); return result ? parseIntValue : defalut; } return defalut; } /// <summary> /// 泛型數組轉換為字符串 /// </summary> /// <typeparam name="T">泛型</typeparam> /// <param name="array">泛型數組</param> /// <param name="delimiter">分隔符</param> /// <returns>轉換好的字符串</returns> public static string ToString<T>(this T[] array, string delimiter) { var data = Array.ConvertAll(array, n => n.ToString()); return string.Join(delimiter, data); } /// <summary> /// 字符串類型轉換 /// </summary> /// <typeparam name="T">目標類型</typeparam> /// <param name="data">需要轉換的字符串</param> /// <returns>轉換類型</returns> public static T ToStringBase<T>(this string data) { var result = default(T); if (!string.IsNullOrEmpty(data)) { var convert = TypeDescriptor.GetConverter(typeof(T)); result = (T)convert.ConvertFrom(data); } return result; } /// <summary> /// 轉換成string類型 /// </summary> /// <param name="data">需要轉換的object</param> /// <param name="defalut">默認數值</param> /// <returns>轉換返回</returns> public static string ToStringOrDefault(this object data, string defalut) { return data == null ? defalut : data.ToString(); } /// <summary> /// 將時間類型轉換為字符串表述 /// </summary> /// <param name="data">DateTime</param> /// <param name="formartString">格式化字符串</param> /// <param name="defalut">默認數值</param> /// <returns>字符串</returns> public static string ToStringOrDefault(this DateTime data, string formartString, string defalut) { if (data != default) return data.ToString(formartString); return defalut; } /// <summary> /// 按照列名稱獲取Sting值 /// </summary> /// <param name="row">DataRow</param> /// <param name="columnName">列名稱</param> /// <param name="defalut">默認數值</param> /// <returns>若列不等于NULL則返回實際值</returns> public static string ToStringOrDefault(this DataRow row, string columnName, string defalut) { if (row != null) defalut = row.IsNull(columnName) ? defalut : row[columnName].ToString(); return defalut; } /// <summary> /// 按照列索引獲取Sting值 /// </summary> /// <param name="row">DataRow</param> /// <param name="columnIndex">列索引</param> /// <param name="defalut">默認數值</param> /// <returns>若列不等于NULL則返回實際值</returns> public static string ToStringOrDefault(this DataRow row, int columnIndex, string defalut) { if (row != null) defalut = row.IsNull(columnIndex) ? defalut : row[columnIndex].ToString().Trim(); return defalut; } /// <summary> /// 轉換成ushort類型 /// </summary> /// <param name="data">需要轉換的object</param> /// <param name="defalut">默認數值</param> /// <returns>轉換返回</returns> public static ushort ToUShortOrDefault(this object data, ushort defalut) { if (data != null) { var result = ushort.TryParse(data.ToString(), out var parseUShortValue); return result ? parseUShortValue : defalut; } return defalut; } /// <summary> /// 將字符串轉換為Guid /// </summary> /// <returns>期待guid</returns> /// <param name="data">數值.</param> public static Guid ToGuidOrDefault(
      江苏快3 沭阳 | 庆阳 | 北海 | 鹤壁 | 汕尾 | 渭南 | 呼伦贝尔 | 景德镇 | 库尔勒 | 开封 | 长垣 | 温岭 | 江西南昌 | 海南 | 天水 | 广州 | 唐山 | 厦门 | 龙岩 | 黄南 | 灌南 | 营口 | 玉树 | 克拉玛依 | 通辽 | 咸阳 | 陇南 | 五家渠 | 广元 | 长垣 | 醴陵 | 吉林长春 | 南阳 | 齐齐哈尔 | 和田 | 雄安新区 | 山西太原 | 铜川 | 孝感 | 鹤壁 | 东台 | 东营 | 偃师 | 大庆 | 蚌埠 | 芜湖 | 怀化 | 吐鲁番 | 咸宁 | 慈溪 | 仁怀 | 扬州 | 塔城 | 厦门 | 佛山 | 文昌 | 临海 | 兴安盟 | 海东 | 玉林 | 张北 | 六安 | 克孜勒苏 | 基隆 | 陕西西安 | 资阳 | 东阳 | 资阳 | 潜江 | 厦门 | 如皋 | 诸城 | 单县 | 松原 | 鄢陵 | 三亚 | 扬中 | 和县 | 桐城 | 厦门 | 芜湖 | 阳江 | 绍兴 | 东台 | 嘉峪关 | 乌海 | 马鞍山 | 吐鲁番 | 吉林 | 东莞 | 安康 | 常州 | 衡阳 | 长葛 | 沧州 | 宝鸡 | 贵州贵阳 | 巴中 | 营口 | 铜陵 | 韶关 | 库尔勒 | 山东青岛 | 厦门 | 湖北武汉 | 孝感 | 咸阳 | 云浮 | 黔南 | 湛江 | 广饶 | 永新 | 招远 |