學習MVC時,學會了寫擴展方法,用起來很方便。
01 | usingSystem; |
02 | usingSystem.Collections.Generic; |
03 | usingSystem.Linq; |
04 | usingSystem.Web; |
05 | usingSystem.Web.Mvc; |
06 | usingSystem.Text; |
07 |
08 | namespaceMvcApp1.Content |
09 | { |
10 | //<%@ Import Namespace="MvcApp1.Content" %> |
11 | publicstaticclassMyHtmlHelper |
12 | { |
13 | publicstaticstringRedLable(thisHtmlHelper helper,stringlbstr) |
14 | { |
15 | StringBuilder sb =newStringBuilder(); |
16 | sb.Append("<span style="color:red;">"); |
17 | sb.Append(lbstr); |
18 | sb.Append("</span>"); |
19 | returnsb.ToString(); |
20 | } |
21 | } |
22 | } |
下面,用上面這個形式來寫我的擴展方法
001 | usingSystem; |
002 | usingSystem.Collections.Generic; |
003 | usingSystem.Text; |
004 |
005 | namespaceTools.Common |
006 | { |
007 | /// <summary> /// 數據優化公共類 |
008 | /// </summary> public static class DataHelper |
009 | { |
010 | #region 根據是否為null或""返回string類型數據 |
011 | /// <summary> /// 根據是否為null或""返回string類型數據 |
012 | /// </summary> /// <param name="obj">要獲取數據的對象 |
013 | /// <param name="defaultValue">為null或""時返回的默認值,不指定則返回"" |
014 | /// <returns></returns> public static string IsNull(this object obj, params object[] defaultValue) |
015 | { |
016 | if(obj ==null||"".Equals(obj.ToString().Trim())) |
017 | { |
018 | if(defaultValue.Length == 0 || defaultValue[0]==null)return""; |
019 | 學習交流
熱門圖片
猜你喜歡的新聞
新聞熱點 2019-10-23 09:17:05
2019-10-21 09:20:02
2019-10-21 09:00:12
2019-09-26 08:57:12
2019-09-25 08:46:36
2019-09-25 08:15:43
疑難解答 |