【干货】通用静态扩展类

好久不见. 提交于 2019-11-27 23:04:17

  在开发过程中,我们通常会将常用方法封装在一个辅助类里,提高可复用性。自.net3.5以后,.net已经支持通过this关键字为类进行扩展,目前只可以扩展静态方法,这对于常用方法的封装是很有用的。比如,给asp.net的Page类扩展WriteJson方法,直接在页面代码里用 this.WriteJson(....),即可轻松调用扩展的静态方法。以下是在工作中积累的部分代码,后续会持续更新,直接上干货:

  1 using System;   2 using System.Collections.Generic;   3 using System.Web;   4 using System.Web.UI;   5 using Newtonsoft.Json;   6 using Newtonsoft.Json.Converters;   7    8 namespace www.ideek.cn   9 {  10     /// <summary>  11     /// 通用静态扩展类  12     /// </summary>  13     public static class ExtendMethod  14     {  15         /// <summary>  16         /// 页面输出Json数据格式  17         /// </summary>  18         /// <param name="page"></param>  19         /// <param name="jsonStr"></param>  20         public static void WriteJson(this Page page, string jsonStr)  21         {  22             page.Response.WriteJson(jsonStr);  23         }  24   25         /// <summary>  26         /// 页面输出字符串  27         /// </summary>  28         /// <param name="page"></param>  29         /// <param name="jsonStr"></param>  30         public static void WriteString(this Page page, string str)  31         {  32             page.Response.WriteString(str);  33         }  34   35         /// <summary>  36         /// 页面输出Json数据格式  37         /// </summary>  38         /// <param name="page"></param>  39         /// <param name="jsonStr"></param>  40         public static void WriteJson(this HttpResponse response, string jsonStr)  41         {  42             response.ContentType = "application/json";  43             response.Charset = "UTF-8";  44             response.Write(jsonStr);  45             response.End();  46         }  47   48         /// <summary>  49         /// 页面输出字符串  50         /// </summary>  51         /// <param name="page"></param>  52         /// <param name="jsonStr"></param>  53         public static void WriteString(this HttpResponse response, string str)  54         {  55             response.ContentType = "text/plain";  56             response.Charset = "UTF-8";  57             response.Write(str);  58             response.End();  59         }  60   61         #region 格式转化  62   63         /// <summary>  64         /// 取日期最小时间  65         /// </summary>  66         /// <param name="dt"></param>  67         /// <returns></returns>  68         public static DateTime GetDateTimeMin(this object dt)  69         {  70             return Convert.ToDateTime(Convert.ToDateTime(dt).ToString("yyyy-MM-dd 00:00:00"));  71         }  72         /// <summary>  73         /// 取日期最大时间  74         /// </summary>  75         /// <param name="dt"></param>  76         /// <returns></returns>  77         public static DateTime GetDateTimeMax(this object dt)  78         {  79             return Convert.ToDateTime(Convert.ToDateTime(dt).ToString("yyyy-MM-dd 23:59:59"));  80         }  81   82         /// <summary>  83         /// 转换为Int型,为空返回0  84         /// </summary>  85         /// <param name="obj"></param>  86         /// <returns></returns>  87         public static int ToIntValue(this object obj)  88         {  89             int result;  90             if (obj == null) return 0;  91             return int.TryParse(obj.ToString().Trim(), out result) ? result : 0;  92         }  93   94         /// <summary>  95         /// 转换为Int64(long)型,为空返回0  96         /// </summary>  97         /// <param name="obj"></param>  98         /// <returns></returns>  99         public static long ToLongValue(this object obj) 100         { 101             long result = 0; 102             if (obj == null) return result; 103             return long.TryParse(obj.ToString().Trim(), out result) ? result : 0; 104         } 105  106         /// <summary> 107         /// 转换为decimal型,为空返回0 108         /// </summary> 109         /// <param name="obj"></param> 110         /// <returns></returns> 111         public static decimal ToDecimalValue(this object obj) 112         { 113             decimal result = 0; 114             if (obj == null) return result; 115             return decimal.TryParse(obj.ToString().Trim(), out result) ? result : 0; 116         } 117  118         /// <summary> 119         /// 转换为float型,为空返回0 120         /// </summary> 121         /// <param name="obj"></param> 122         /// <returns></returns> 123         public static float ToFloatValue(this object obj) 124         { 125             float result = 0; 126             if (obj == null) return result; 127             return float.TryParse(obj.ToString().Trim(), out result) ? result : 0; 128         } 129  130         /// <summary> 131         /// 转为String类型,为null返回"" 132         /// </summary> 133         /// <param name="obj"></param> 134         /// <returns></returns> 135         public static string ToStringValue(this object obj) 136         { 137             if (obj == null) return string.Empty; 138             return obj.ToString().Trim(); 139         } 140  141         /// <summary> 142         /// 转为DateTime类型,为null返回当前时间 143         /// </summary> 144         /// <param name="obj"></param> 145         /// <returns></returns> 146         public static DateTime ToDateTimeValue(this object obj) 147         { 148             if (obj == null) return DateTime.Now; 149             return Convert.ToDateTime(obj); 150         } 151  152         /// <summary> 153         /// 转为DateTime?类型 154         /// </summary> 155         /// <param name="obj"></param> 156         /// <returns></returns> 157         public static DateTime? ToDateTime(this object obj) 158         { 159             var result = new Nullable<DateTime>(); 160             if (!string.IsNullOrEmpty(obj.ToStringValue())) 161             { 162                 DateTime dt; 163                 if (DateTime.TryParse(obj.ToString().Trim(), out dt)) 164                     result = dt; 165             } 166             return result; 167         } 168  169         #endregion 170     } 171 }

  其中,页面输出采用Newtonsoft.Json开源类库,关于Newtonsoft.Json的用法,请参考:https://github.com/JamesNK/Newtonsoft.Json

 

 爱戴客 - 可穿戴设备第一门户
 网址:www.ideek.cn
 微博:www.weibo.com/ideek
 为可穿戴设备爱好者提供最新、最专业的可穿戴设备产品和资讯信息。

 

转载于:https://www.cnblogs.com/schwann/p/ExtendMethod.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!