using System; using System.Collections.Generic; using System.ComponentModel; using System.Reflection; using System.Text; namespace LiveForum.Code.Utility { public static class Convert { public static long tolong(this object obj) { long tmp = 0; long.TryParse(obj.tostr(), out tmp); return tmp; } public static decimal todec(this object obj) { decimal tmp = 0; decimal.TryParse(obj.tostr(), out tmp); return tmp; } public static string tostr(this object obj) { if (obj == null) return string.Empty; return obj.ToString(); } public static string tostrnotempty(this object obj, string defaultrStr = "") { if (obj == null) return defaultrStr; if (string.IsNullOrEmpty(obj.ToString())) { return defaultrStr; } return obj.ToString(); } public static byte[] strToutf8Byte(this String obj) { var objstr = obj.tostr(); return Encoding.UTF8.GetBytes(objstr); } public static string utf8ByteTostr(this byte[] obj) { return Encoding.UTF8.GetString(obj); } /// /// 获取首字母(TODO:待实现拼音功能) /// /// /// public static string toSpell(this object obj) { return obj.tostr(); } public static DateTime? toDateTime(this string str) { try { return System.Convert.ToDateTime(str); } catch (Exception) { return null; } } public static double? todouble(this string str) { try { return System.Convert.ToDouble(str); } catch (Exception) { return null; } } public static short? toshort(this string str) { try { return System.Convert.ToInt16(str); } catch (Exception) { return null; } } public static int toint(this string str, int defaultInt = 0) { try { return System.Convert.ToInt32(str); } catch (Exception) { return defaultInt; } } public static bool? tobool(this string str) { try { return System.Convert.ToBoolean(str); } catch (Exception) { return null; } } } }