Common方法
一些Common的方法。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Script.Serialization; 6 using System.IO; 7 using System.Runtime.Serialization.Json; 8 using System.Data; 9 using System.Xml; 10 using System.Text; 11 using System.Xml.Serialization; 12 using System.Reflection; 13 14 namespace Commom 15 { 16 public static class CommonMethod 17 { 18 public static List<T> JSONStringToList<T>(this string JsonStr) 19 { 20 JavaScriptSerializer Serializer = new JavaScriptSerializer(); 21 List<T> objs = Serializer.Deserialize<List<T>>(JsonStr); 22 return objs; 23 } 24 25 public static string ConvertToJson(System.Data.DataTable dt) 26 { 27 System.Text.StringBuilder retVal = new System.Text.StringBuilder(); 28 retVal.Append("["); 29 int RowCount = 0, ColumnCount = 0; 30 foreach (DataRow row in dt.Rows) 31 { 32 RowCount++; 33 retVal.Append("{"); 34 foreach (DataColumn column in dt.Columns) 35 { 36 ColumnCount++; 37 retVal.AppendFormat("'{0}':'{1}'{2}", column.ColumnName, (row[column].ToString()), ColumnCount == dt.Columns.Count ? "" : ","); 38 } 39 ColumnCount = 0; 40 retVal.Append("}"); 41 retVal.AppendFormat("{0}", RowCount == dt.Rows.Count ? "" : ","); 42 } 43 retVal.Append("]"); 44 return retVal.ToString(); 45 } 46 47 public static string ToJSONS(object obj) 48 { 49 JavaScriptSerializer serillizer = new JavaScriptSerializer(); 50 return serillizer.Serialize(obj); 51 } 52 53 public static string ToJSONS(object obj, int recursiondepth) 54 { 55 JavaScriptSerializer serialize = new JavaScriptSerializer(); 56 serialize.RecursionLimit = recursiondepth; 57 return serialize.Serialize(obj); 58 } 59 60 public static string DtToXml(DataTable dt) 61 { 62 if (dt != null) 63 { 64 if (string.IsNullOrEmpty(dt.TableName)) 65 dt.TableName = Guid.NewGuid().ToString(); 66 67 MemoryStream ms = null; 68 XmlTextWriter XmlWt = null; 69 try 70 { 71 ms = new MemoryStream(); 72 XmlWt = new XmlTextWriter(ms, Encoding.Unicode);//根据ms实例化XmlWt 73 dt.WriteXml(XmlWt); 74 int count = (int)ms.Length; 75 byte[] temp = new byte[count]; 76 ms.Seek(0, SeekOrigin.Begin); 77 ms.Read(temp, 0, count); 78 UnicodeEncoding ucode = new UnicodeEncoding(); 79 string returnValue = ucode.GetString(temp); 80 return returnValue; 81 } 82 catch (System.Exception ex) 83 { 84 throw ex; 85 } 86 finally 87 { 88 if (XmlWt != null)//释放资源 89 { 90 XmlWt.Close(); 91 ms.Close(); 92 ms.Dispose(); 93 } 94 } 95 } 96 else 97 { 98 return ""; 99 } 100 } 101 102 103 public static int ToLength(this string input) 104 { 105 if (input.IsEmpty()) 106 return 0; 107 int n = 0; 108 foreach (char c in input) 109 { 110 if ((int)c > 256) 111 n += 2; 112 else 113 n++; 114 } 115 return n; 116 } 117 118 public static bool IsEmpty(this string input) 119 { 120 return string.IsNullOrEmpty(input); 121 } 122 123 124 /// <summary> 125 /// 反序列化 126 /// </summary> 127 /// <param name="type">对象类型</param> 128 /// <param name="filename">文件路径</param> 129 public static object Load(Type type, string filename) 130 { 131 FileStream fs = null; 132 try 133 { 134 // open the stream... 135 fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 136 XmlSerializer serializer = new XmlSerializer(type); 137 return serializer.Deserialize(fs); 138 } 139 catch (Exception ex) 140 { 141 throw ex; 142 } 143 finally 144 { 145 if (fs != null) 146 fs.Close(); 147 } 148 } 149 150 /// <summary> 151 /// 序列化 152 /// </summary> 153 /// <param name="obj">对象</param> 154 /// <param name="filename">文件路径</param> 155 public static void Save(object obj, string filename) 156 { 157 FileStream fs = null; 158 // serialize it... 159 try 160 { 161 fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); 162 XmlSerializer serializer = new XmlSerializer(obj.GetType()); 163 serializer.Serialize(fs, obj); 164 } 165 catch (Exception ex) 166 { 167 throw ex; 168 } 169 finally 170 { 171 if (fs != null) 172 fs.Close(); 173 } 174 } 175 176 } 177 178 /// <summary> 179 /// 单例实现辅助类 180 /// </summary> 181 /// <typeparam name="T"></typeparam> 182 public static class Singleton<T> where T : class 183 { 184 static volatile T _instance; 185 186 static object _lock = new object(); 187 188 static Singleton() { } 189 190 /// <summary> 191 /// 创建/获取一个可以new的类对象的单件实例 192 /// </summary> 193 public static T Instance 194 { 195 get 196 { 197 if (_instance == null) 198 { 199 lock (_lock) 200 { 201 if (_instance == null) 202 { 203 ConstructorInfo constructor = null; 204 try 205 { 206 //构造函数不包含public修饰符的 207 constructor = typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[0], null); 208 } 209 catch (Exception ex) 210 { 211 throw new InvalidOperationException(ex.Message, ex); 212 } 213 if (constructor == null || constructor.IsAssembly) 214 { 215 throw new InvalidOperationException(string.Format("在'{0}'里面没有找到private或者protected的构造函数。", typeof(T).Name)); 216 } 217 _instance = (T)constructor.Invoke(null); 218 } 219 } 220 } 221 return _instance; 222 } 223 } 224 } 225 226 227 }