Visual Studio Orcas之擴充方法小試牛刀

來源:互聯網
上載者:User

Visual Studio Codename "Orcas"有一個新特性是擴充方法(Extender Method).

首先來好好理解一下什麼是擴充方法:從字面上看,首先它屬於方法的範疇,但為什麼前面有一個擴充呢,可以簡單的這樣理解"就是新增加的"意思.也就是對於.NET架構下的FCL,微軟的工程師已經為我們開發了一些通用的方法,直接調用就可以了,這在給我們帶來方便的同時,也極大的限制了FLC類的靈活性,除了繼承,我想不出更好的擴充類的方法,可能就是由於這一點,在oracs中引入了擴充方法這一特性。自然是為了更好的解決上面的問題,當然關於擴充方法如何促進LINQ的應用,你可以找LINQ的一些資料去學習一下。

知道了什麼是擴充方法,下面來看一下,如何定義或者叫實現擴充方法(僅限於C#3.0)。第一就是要定義一個靜態類(不能夠被執行個體化,只能包含靜態成員)。第二就是要在該靜態類中加入一個靜態方法(擴充方法),第三就是一定要將靜態方法的第一個參數的類型前用this關鍵字。第一個參數就是該方法依託的類型(或者說該方法要擴充的類型),當然,擴充方法,還可以有其它的參數,但只能放在第一個特殊的參數之後)。

下面來看一個具體實現的例子(當然很簡單),具體的擴充和應用場合的選擇要靠大家了。

在該類庫中定義擴充方法,如果你的擴充方法的定義和測試擴充方法分別定義在不同的項目中,那麼要將靜態類和靜態方法都要聲明成public。
/* function description:display how to use extend method which is embeded
 *                      as a new function character in the next version of
 *                      visual studio codename 'oracs'
 * author:haohai fu
 * Date:2007-6-14
 * Version:1.0
 * Details:Make sure that you must define a new public static class in a namespace
 *         and define a static method as Extender,of which the first parameter must be add
 *         this key word before the type.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary1
{
    public static class Class1
    {
        public static string getDate(this string s,string append)
        {
            return s + append;
        }
    }
}

這是是測試專案。
/* function description:this is an application entry.display how to use extend method which is embeded
 *                      as a new function character in the next version of
 *                      visual studio codename 'oracs'
 * author:haohai fu
 * Date:2007-6-14
 * Version:1.0
 * Details:Please pay attention to how to call the extender method.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    //using ConsoleApplication1;
    using ClassLibrary1;//通過添加引用,選擇項目選項卡(為什麼不選擇第一個選項卡,有興趣的讀者可以找微軟的軟體項目架構的書來看看)
    class Program
    {
        static void Main(string[] args)
        {        
            Console.WriteLine ("Please input four number:");
            string year = Console.ReadLine();
            Console.WriteLine("Please input a number between 1 and 12:");
            string month = Console.ReadLine();
            Console.WriteLine("Please input a munber between 1 and 31:");
            string day = Console.ReadLine();
            Console.WriteLine(year.getDate("nian").getDate(month + "yue").getDate(day + "ri"));
            Console.ReadLine();
        }
    }
}

輸出的結果是這樣的(虛擬機器環境,不支援中文):

Please input four number:
2007
Please input a number between 1 and 12:
6
Please input a munber between 1 and 31:
14
2007nian6yue14ri

最後特別要強調的是擴充方法的調用,大家可以參看上面的代碼來體驗其靈活性。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.