c#写扩展方法学习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