一、引用
1 using Newtonsoft.Json; 2 using System; 3 using System.Collections.Generic; 4 using System.Data; 5 using System.IO; 6 using System.Linq; 7 using System.Reflection; 8 using System.Text; 9 using System.Web.Script.Serialization;
二、使用
1、Json
1 /// <summary> 2 /// 转换成json格式字符串 3 /// </summary> 4 /// <param name="obj"></param> 5 /// <returns></returns> 6 public static string Serialize(object obj) 7 { 8 JavaScriptSerializer js = new JavaScriptSerializer(); 9 string reponse = js.Serialize(obj); 10 return reponse; 11 } 12 13 /// <summary> 14 /// JSON格式字符串转换成实体 15 /// </summary> 16 /// <typeparam name="T"></typeparam> 17 /// <param name="str"></param> 18 /// <returns></returns> 19 public static T Deserialize<T>(string str) 20 { 21 JavaScriptSerializer js = new JavaScriptSerializer(); 22 T obj = js.Deserialize<T>(str); 23 return obj; 24 } 25 26 /// <summary> 27 /// 不循环引用的ToJson 28 /// </summary> 29 /// <param name="obj"></param> 30 /// <returns></returns> 31 public static string ToJsonString(this Object obj) 32 { 33 JsonSerializerSettings jsSettings = new JsonSerializerSettings(); 34 jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; 35 return JsonConvert.SerializeObject(obj, jsSettings); 36 } 37 38 /// <summary> 39 /// 支持自我循环引用的ToJson 40 /// </summary> 41 /// <param name="obj"></param> 42 /// <returns></returns> 43 public static string ToJson(this object obj) 44 { 45 return JsonConvert.SerializeObject(obj); 46 } 47 48 /// <summary> 49 /// 反json 50 /// </summary> 51 /// <typeparam name="Tresult">反json后的格式</typeparam> 52 /// <param name="obj"></param> 53 /// <returns></returns> 54 public static Tresult FromJson<Tresult>(this string obj) 55 { 56 JavaScriptSerializer js = new JavaScriptSerializer(); 57 js.MaxJsonLength = Int32.MaxValue; 58 Tresult t = js.Deserialize<Tresult>(obj); 59 return JsonConvert.DeserializeObject<Tresult>(obj); 60 }
2、实体类
1 /// <summary> 2 /// 字典转为实体类 3 /// </summary> 4 /// <typeparam name="T"></typeparam> 5 /// <param name="dic"></param> 6 /// <returns></returns> 7 public static T ConvertDic<T>(Dictionary<string, string> dic) 8 { 9 T model = Activator.CreateInstance<T>(); 10 PropertyInfo[] modelPro = model.GetType().GetProperties(); 11 if (modelPro.Length > 0 && dic.Any()) 12 { 13 foreach (var item in modelPro) 14 { 15 if (!dic.ContainsKey(item.Name)) continue; 16 if (item.PropertyType == typeof(string)) 17 { 18 item.SetValue(model, dic[item.Name], null); 19 } 20 else if (item.PropertyType == typeof(Int32)) 21 { 22 item.SetValue(model, Convert.ToInt32(dic[item.Name]), null); 23 } 24 else if (item.PropertyType == typeof(DateTime)) 25 { 26 item.SetValue(model, Convert.ToDateTime(dic[item.Name]), null); 27 } 28 } 29 } 30 return model; 31 } 32 33 /// <summary> 34 /// 填充对象列表:用DataTable填充实体类 35 /// </summary> 36 public static List<T> FillModel<T>(DataTable dt, T model) 37 { 38 if (dt == null || dt.Rows.Count == 0) 39 { 40 return null; 41 } 42 List<T> modelList = new List<T>(); 43 foreach (DataRow dr in dt.Rows) 44 { 45 for (int i = 0; i < dr.Table.Columns.Count; i++) 46 { 47 PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName); 48 if (propertyInfo != null && dr[i] != DBNull.Value) 49 propertyInfo.SetValue(model, dr[i], null); 50 } 51 52 modelList.Add(model); 53 } 54 return modelList; 55 } 56 57 /// <summary> 58 /// 填充对象:用DataRow填充实体类 59 /// </summary> 60 public static T FillModel<T>(DataRow dr,T model) 61 { 62 if (dr == null) 63 { 64 return default(T); 65 } 66 for (int i = 0; i < dr.Table.Columns.Count; i++) 67 { 68 PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName); 69 if (propertyInfo != null && dr[i] != DBNull.Value) 70 propertyInfo.SetValue(model, dr[i], null); 71 } 72 return model; 73 } 74 75 /// <summary> 76 /// 实体类转换成DataSet 77 /// </summary> 78 /// <param name="modelList">实体类列表</param> 79 /// <returns></returns> 80 public static DataSet FillDataSet<T>(List<T> modelList) 81 { 82 if (modelList == null || modelList.Count == 0) 83 { 84 return null; 85 } 86 else 87 { 88 DataSet ds = new DataSet(); 89 ds.Tables.Add(FillDataTable(modelList)); 90 return ds; 91 } 92 } 93 94 /// <summary> 95 /// 实体类转换成DataTable 96 /// </summary> 97 /// <param name="modelList">实体类列表</param> 98 /// <returns></returns> 99 public static DataTable FillDataTable<T>(List<T> modelList) 100 { 101 if (modelList == null || modelList.Count == 0) 102 { 103 return null; 104 } 105 DataTable dt = CreateData(modelList[0]); 106 107 foreach (T model in modelList) 108 { 109 DataRow dataRow = dt.NewRow(); 110 foreach (PropertyInfo propertyInfo in typeof(T).GetProperties()) 111 { 112 dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null); 113 } 114 dt.Rows.Add(dataRow); 115 } 116 return dt; 117 } 118 119 /// <summary> 120 /// 根据实体类得到表结构 121 /// </summary> 122 /// <param name="model">实体类</param> 123 /// <returns></returns> 124 private static DataTable CreateData<T>(T model) 125 { 126 DataTable dataTable = new DataTable(typeof(T).Name); 127 foreach (PropertyInfo propertyInfo in typeof(T).GetProperties()) 128 { 129 dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType)); 130 } 131 return dataTable; 132 }
3、其他
1 /// <summary> 2 /// 转换为标准的日期格式 3 /// </summary> 4 /// <param name="obj"></param> 5 /// <returns></returns> 6 public static DateTime ToDateTime(this object obj) 7 { 8 return Convert.ToDateTime(obj); 9 } 10 /// <summary> 11 /// 转换为标准的十进制 12 /// </summary> 13 /// <param name="obj"></param> 14 /// <returns></returns> 15 public static decimal ToDecimal(this object obj) 16 { 17 return Convert.ToDecimal(obj); 18 } 19 /// <summary> 20 /// 转换为标准的32位整数 21 /// </summary> 22 /// <param name="obj"></param> 23 /// <returns></returns> 24 public static int ToInt(this object obj) 25 { 26 return Convert.ToInt32(obj); 27 } 28 /// <summary> 29 /// 转换为标准的64位整数 30 /// </summary> 31 /// <param name="obj"></param> 32 /// <returns></returns> 33 public static long ToLong(this object obj) 34 { 35 return Convert.ToInt64(obj); 36 } 37 /// <summary> 38 /// 转换为布尔值 39 /// </summary> 40 /// <param name="obj"></param> 41 /// <returns></returns> 42 public static bool ToBool(this object obj) 43 { 44 return Convert.ToBoolean(obj); 45 } 46 /// <summary> 47 /// 转换为双精度小数 48 /// </summary> 49 /// <param name="obj"></param> 50 /// <returns></returns> 51 public static double ToDouble(this object obj) 52 { 53 return Convert.ToDouble(obj); 54 } 55 /// <summary> 56 /// 将数字转换为英语 57 /// </summary> 58 /// <param name="number"></param> 59 /// <returns></returns> 60 public static string NumberToEnglishString(int number) 61 { 62 if (number < 0) //暂不考虑负数 63 { 64 return ""; 65 } 66 if (number < 20) //0到19 67 { 68 switch (number) 69 { 70 case 0: 71 return "zero"; 72 case 1: 73 return "one"; 74 case 2: 75 return "second"; 76 case 3: 77 return "third"; 78 case 4: 79 return "four"; 80 case 5: 81 return "five"; 82 case 6: 83 return "six"; 84 case 7: 85 return "seven"; 86 case 8: 87 return "eight"; 88 case 9: 89 return "nine"; 90 case 10: 91 return "ten"; 92 case 11: 93 return "eleven"; 94 case 12: 95 return "twelve"; 96 case 13: 97 return "thirteen"; 98 case 14: 99 return "fourteen"; 100 case 15: 101 return "fifteen"; 102 case 16: 103 return "sixteen"; 104 case 17: 105 return "seventeen"; 106 case 18: 107 return "eighteen"; 108 case 19: 109 return "nineteen"; 110 default: 111 return ""; 112 } 113 } 114 if (number < 100) //20到99 115 { 116 if (number % 10 == 0) //20,30,40,...90的输出 117 { 118 switch (number) 119 { 120 case 20: 121 return "twenty"; 122 case 30: 123 return "thirty"; 124 case 40: 125 return "forty"; 126 case 50: 127 return "fifty"; 128 case 60: 129 return "sixty"; 130 case 70: 131 return "seventy"; 132 case 80: 133 return "eighty"; 134 case 90: 135 return "ninety"; 136 default: 137 return ""; 138 } 139 } 140 else //21.22,....99 思路:26=20+6 141 { 142 return string.Format("{0} {1}", NumberToEnglishString(10 * (number / 10)), 143 NumberToEnglishString(number % 10)); 144 } 145 } 146 if (number < 1000) //100到999 百级 147 { 148 if (number % 100 == 0) 149 { 150 return string.Format("{0} hundred", NumberToEnglishString(number / 100)); 151 } 152 else 153 { 154 return string.Format("{0} hundred and {1}", NumberToEnglishString(number / 100), 155 NumberToEnglishString(number % 100)); 156 } 157 } 158 if (number < 1000000) //1000到999999 千级 159 { 160 if (number % 1000 == 0) 161 { 162 return string.Format("{0} thousand", NumberToEnglishString(number / 1000)); 163 } 164 else 165 { 166 return string.Format("{0} thousand and {1}", NumberToEnglishString(number / 1000), 167 NumberToEnglishString(number % 1000)); 168 } 169 } 170 if (number < 1000000000) //1000 000到999 999 999 百万级 171 { 172 if (number % 1000 == 0) 173 { 174 return string.Format("{0} million", NumberToEnglishString(number / 1000000)); 175 } 176 else 177 { 178 return string.Format("{0} million and {1}", NumberToEnglishString(number / 1000000), 179 NumberToEnglishString(number % 1000000)); 180 } 181 } 182 if (number <= int.MaxValue) //十亿 级 183 { 184 if (number % 1000000000 == 0) 185 { 186 return string.Format("{0} billion", NumberToEnglishString(number / 1000000000)); 187 } 188 else 189 { 190 return string.Format("{0} billion and {1}", NumberToEnglishString(number / 1000000000), 191 NumberToEnglishString(number % 1000000000)); 192 } 193 } 194 return ""; 195 } 196 /// <summary> 197 /// 文件转Base64 198 /// </summary> 199 /// <param name="filePath"></param> 200 /// <returns></returns> 201 public static string FileToBase64(string filePath) 202 { 203 FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); 204 byte[] buffur = new byte[fs.Length]; 205 fs.Read(buffur, 0, (int)fs.Length); 206 return Convert.ToBase64String(buffur); 207 }