C#高级编程 - 小鸟系列之常用方法
来源:互联网 发布:淘宝上情趣内衣买家秀 编辑:程序博客网 时间:2024/05/14 19:24
这里汇总下C#的常用方法,后续会不断更新
序列化
添加引用Newtonsoft.Json.dll
public static string JSONSerializeObject(object oObject){ return JsonConvert.SerializeObject(oObject);}
反序列化
添加引用Newtonsoft.Json.dll
public static object JSONDeserializeObject<T>(string value){ return JsonConvert.DeserializeObject<T>(value, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });}
获取时间戳
public static long getTimeStamp(this DateTime dt){ var start = new DateTime(1970, 1, 1, 0, 0, 0, dt.Kind); return Convert.ToInt64((dt - start).TotalSeconds);}
将DataTable转换成对象(List<T>)
public static List<T> toList<T>(this DataTable dt) where T : class, new(){ List<T> oList = new List<T>(); Type type = typeof(T); string tempName = string.Empty; foreach (DataRow dr in dt.Rows) { T t = new T(); PropertyInfo[] propertys = t.GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { tempName = pi.Name; if (dt.Columns.Contains(tempName)) { if (!pi.CanWrite) continue; object value = dr[tempName]; if (value != DBNull.Value) pi.SetValue(t, value, null); } } oList.Add(t); } return oList;}调用:
List<Persion> _persion = dtList.toList<Persion>();
SHA1加密
public static string EncryptToSHA1(string str){ SHA1 sha1 = new SHA1CryptoServiceProvider(); byte[] bytes_sha1_in = UTF8Encoding.Default.GetBytes(str); byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in); string str_sha1_out = BitConverter.ToString(bytes_sha1_out); str_sha1_out = str_sha1_out.Replace("-", ""); return str_sha1_out;}
SHA512加密
public static string EncryptToSHA512(string str){ var sha = new SHA512Managed(); var bytes = Encoding.UTF8.GetBytes(str); var encryptedBytes = sha.ComputeHash(bytes); var encryptedInput = BitConverter.ToString(encryptedBytes); encryptedInput = encryptedInput.Replace("-", ""); return encryptedInput;}模拟Http发送Post或Get请求
public static string SendRequest(string requestUrl, string data, string contentType = "application/json", string requestMethod = WebRequestMethods.Http.Post, int connectionLimit = 100){ HttpWebRequest httpWebRequest = null; string returnData = ""; try { httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUrl); httpWebRequest.Method = requestMethod; httpWebRequest.ContentType = "application/json; charset=utf-8"; httpWebRequest.Proxy = null; httpWebRequest.KeepAlive = false; httpWebRequest.Timeout = 1000 * 25; httpWebRequest.Headers.Add("Origin", "SIS API"); httpWebRequest.ServicePoint.ConnectionLimit = connectionLimit; httpWebRequest.ServicePoint.UseNagleAlgorithm = false; httpWebRequest.ServicePoint.ConnectionLimit = 100; httpWebRequest.AllowWriteStreamBuffering = false; httpWebRequest.Accept = "application/json"; byte[] dataArray = Encoding.UTF8.GetBytes(data); httpWebRequest.ContentLength = dataArray.Length; using (Stream requestStream = httpWebRequest.GetRequestStream()) { requestStream.Write(dataArray, 0, dataArray.Length); HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()); returnData = streamReader.ReadToEnd(); streamReader.Close(); httpWebResponse.Close(); } httpWebRequest.Abort(); } catch (Exception ex) { throw ex; } finally { if (httpWebRequest != null) { httpWebRequest.Abort(); httpWebRequest = null; } } return returnData;}
0 0
- C#高级编程 - 小鸟系列之常用方法
- C#高级编程 - 小鸟系列之反射的用法
- C#高级编程 - 小鸟系列之代码段管理
- c#之跟踪(c#高级编程系列)
- Android高级编程(奋斗之小鸟)_PDF 电子书
- asp.net MVC - 小鸟系列之自定义HTML辅助方法
- C#高级编程(中文第七版)(奋斗的小鸟)_PDF 电子书
- C#高级编程之“反射”
- C#高级编程之“反射”
- c#高级编程之常量
- 关于C#高级编程读书笔记系列
- 《C#高级编程》笔记系列第三弹
- C#高级编程之C#语言
- C#编程技巧之常用文件名、路径处理方法
- C#编程技巧之常用文件名、路径处理方法
- C#编程技巧之常用文件名、路径处理方法(ZT)
- c#网络编程常用特性之方法回调
- Chap13:JNI编程系列之高级篇
- hibernate通过数据库表反向生成实体类
- PHP常规正则验证
- [POJ 1811 Prime Test] Miller_Rabin + Pollard_rho 大数质数判断/质因子分解模板
- poj 2253 Frogger(变式Dijkstra)
- MySQL客户端工具
- C#高级编程 - 小鸟系列之常用方法
- 带你玩转Visual Studio——调用约定__cdecl、__stdcall和__fastcall
- 初识测试架构之单机架构
- mysql delete无法释放空间用分区代替
- hadoop2.x学习资料
- Maven 环境下使用 proguard-maven-plugin 插件混淆你的源码
- 把数组排成最小的数
- 栈(Stack)、队列(Queue)与包(Bag)的实现
- 贪心+数学——2016 (湖南省第十二届大学生计算机程序设计竞赛 A)