本篇文章为大家展示了 静态类在c#中使用场景有哪些,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
comm类就是静态的Static
public class fnComm { public static JObject Post(HttpClient myhttp, string url, JObject json) { HttpContent content = new StringContent(JsonConvert.SerializeObject(json), Encoding.UTF8, "application/json"); var message = Task<HttpResponseMessage>.Run<HttpResponseMessage>(() => { return myhttp.PostAsync(url, content); }); message.Wait(); //接收返回得信息 if (message.Result.IsSuccessStatusCode) { var s = Task.Run(() => { return message.Result.Content.ReadAsStringAsync(); }); s.Wait(); return JObject.Parse(s.Result); } else { throw new Exception("StatusCode:" + message.Result.StatusCode.ToString()); } } public static byte[] ConvertToByteAry(object obj) { var j = JsonConvert.SerializeObject(obj); var ary = System.Text.Encoding.UTF8.GetBytes(j); return ary; } /// <summary> /// datetime转换为unixtime /// </summary> /// <param name="time"></param> /// <returns></returns> public static int TimeToUnixTime(System.DateTime time) { return (int)(time - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds; } public static string GenerateTransId(int i) { string transId = DateTime.Now.ToString("yyyyMMddHHmmss"); int l = i - 14; return transId + CreateRandCode(l); } public static string CreateRandCode(int codeLen) { string codeSerial = "1,2,3,4,5,6,7,a,c,d,e,f,h,i,j,k,m,n,p,r,s,t,A,C,D,E,F,G,H,J,K,M,N,P,Q,R,S,U,V,W,X,Y,Z"; if (codeLen == 0) { codeLen = 16; } string[] arr = codeSerial.Split(','); string code = ""; int randValue = -1; Random rand = new Random(unchecked((int)DateTime.Now.Ticks)); for (int i = 0; i < codeLen; i++) { randValue = rand.Next(0, arr.Length - 1); code += arr[randValue]; } return code; } public static string GetStringFromList(List<string> list) { StringBuilder sb = new StringBuilder(); string strReturn; if (list.Count > 0) { foreach (string item in list) { sb.AppendFormat("'{0}',", item); } strReturn = sb.ToString(0, sb.Length - 1); } else { strReturn = "''"; } return strReturn; } }
上述内容就是 静态类在c#中使用场景有哪些,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。