c#扩展方法的使用

扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。

1.为什么会有扩展方法

你一定很疑问什么是扩展方法!什么是扩展方法?回答这个问题之前,先看看我们一般情况下方法的调用。类似这样的通用方法你一定写过:

static void Main(string[] args)
{
   string strRes = "2013-09-08 14:12:10";
   var dRes = GetDateTime(strRes);
}
//将字符串转换为日期
public static DateTime GetDateTime(string strDate)
{
   return Convert.ToDateTime(strDate);
}
//得到非空的字符串
public static string GetNotNullStr(string strRes)
{
   if (strRes == null)
     return string.Empty;
   else
     return strRes;
}
或者在项目中有一个类似Utils的工具类,里面有多个Helper,例如StringHelper、XmlHelper等等,每个Helper里面有多个static的通用方法,然后调用的时候就是StringHelper.GetNotNullStr(“aa”);这样。还有一种普通的用法就是new 一个对象,通过对象去调用类里面的非static方法。反正博主刚开始做项目的时候就是这样写的。后来随着工作经验的累积,博主看到了扩展方法的写法,立马就感觉自己原来的写法太Low了。进入正题。

这是msdn上说的,也就是你可以对String,Int,DataRow,DataTable等这些类型的基础上增加一个或多个方法,使用时不需要去修改或编译类型本身的代码。

先做个例子吧,以String为例,需要在字符串类型中加一个从字符串转为数值的功能。

以往我们可能是这样做的,会专门写一个方法做过转换

public static int StrToInt(string s)
{
    intid;
    int.TryParse(s,outid);//这里当转换失败时返回的id为0
    returnid;
}

调用就使用

string s = "abc";
int i = StrToInt(s)

若是String类型有一个名为ToInt()(从字符串转为数值)的方法,就可以这样调用了

string s = "abc";
int i = s.ToInt();

这样看起来是不是更好,下面来看看具体怎么实现

2.如何写扩展方法

第一步:

我先创建一个解决方案,一个web应用程序(webtest)及一个类库(W.Common)

在webtest项目添加引用W.Common项目

第二步:在类库中新建一个名为EString.cs类

看了上面的代码了吧,扩展方法规定类必须是一个静态类,EString是一个静态类,里面包含的所有方法都必须是静态方法。

msdn是这样规定扩展方法的:“扩展方法被定义为静态方法,但它们是通过实例方法语法进行调用的。 它们的第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀。

3.如何理解扩展方法

EString里有一个ToInt的静态方法,他接收一个自身参数this,类型为string,this string必须在方法参数的第一个位置。

这句话什么意思,即你需要对string扩展一个ToInt方法,this是string实例化后的对象,这可能说的不太清楚,我的表述能力能弱,不要见怪呀。。。通俗的说就是,扩展方法跟静态类的名称无关,只需要在一个静态类里面定义一个静态方法,第一个参数必须this string开头。

如果需要你要对DateTime类型扩展方法名为IsRange(判断是否在此时间范围内),代码如下:

/// <summary>
   /// 此时间是否在此范围内 -1:小于开始时间 0:在开始与结束时间范围内 1:已超出结束时间
   /// </summary>
   /// <param name="t"></param>
   /// <param name="startTime"></param>
   /// <param name="endTime"></param>
   /// <returns></returns>
   publicstaticintIsRange(thisDateTime t, DateTime startTime, DateTime endTime)
   {
       if(((startTime - t).TotalSeconds > 0))
       {
           return-1;
       }
 
       if(((endTime - t).TotalSeconds < 0))
       {
           return1;
       }
 
       return0;
   }

这里的扩展方法是用this DateTime打头,那么就可以这样调用

time.IsRange(t1,t2);//判断时间time是否在t1到t2的范围内

当前代码在使用扩展方法前需要先引用命名空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using W.Common;//这里引用扩展方法所在的命名空间
 
namespacewebtest
{
    publicpartialclass_Default : System.Web.UI.Page
    {
        protectedvoidPage_Load(objectsender, EventArgs e)
        {
            use1();
            Response.Write("<br />");
            use2();
        }
 
        /// <summary>
        /// 没有用扩展方法
        /// </summary>
        privatevoiduse1()
        {
            strings ="abc";
            inti = StrToInt(s);
            Response.Write("没有用扩展方法:"+ i);
        }
 
        /// <summary>
        /// 使用扩展方法
        /// </summary>
        privatevoiduse2()
        {
            strings ="2012";
            inti = s.ToInt();
            Response.Write("使用扩展方法:"+ i);
        }
 
        publicstaticintStrToInt(strings)
        {
            intid;
            int.TryParse(s,outid);//这里当转换失败时返回的id为0
            returnid;
        }
    }
}

这我第一次写文章算是排过版的,用好长时间呀,以前都只是看别人的文章,现在才知道写一篇好的文章真的不容易呀。不要脸其实你就是抄的

原文地址:https://www.cnblogs.com/zhangmumu/p/7374833.html