C# 3.0 - Extension Methods 練習

C# 3.0 - Extension Methods 練習

C# 3.0 的新語言特性 Extension Methods,可以讓程式設計師對現有的類別增加額外的靜態方法。

以下 Extension Methods 範例,對系統 string 類別擴充 ToMD5() 方法與 ToSHA1() 方法,對 int 類別擴充 Square() Cube() 方法。

首先,建立自訂命名 MyEctensionMethods 的命名空間,並建立自訂命名 StringExtensionMethods 靜態類別。最後,建立 public static string ToMD5(this string source) 的靜態方法,其中參數必須以 this string s 方式宣告:以 this 開頭,中間是符合擴充類別 stirng 的型態,最後是自訂變數名稱 source

   1:  namespace MyExtensionMethods
   2:  {
   3:      /// <summary>
   4:      /// 字串型態的Extension Methods
   5:      /// </summary>
   6:      public static class StirngExtensionMethods
   7:      {
   8:          /// <summary>
   9:          /// 取得 MD5 加密字串
  10:          /// </summary>
  11:          public static string ToMD5(this string source)
  12:          {
  13:              MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  14:              byte[] bytValue = Encoding.UTF8.GetBytes(source);
  15:              byte[] bytHash = md5.ComputeHash(bytValue);
  16:              md5.Clear();
  17:   
  18:              StringBuilder sb = new StringBuilder();
  19:              for (int i = 0; i < bytHash.Length; i++)
  20:              {
  21:                  sb.Append(bytHash[i].ToString("X").PadLeft(2, '0'));
  22:              }
  23:   
  24:              return sb.ToString().ToLower();
  25:          }
  26:      }
  27:  }

 

 

 

完整範例,包含系統 string 類別擴充 ToMD5() 方法與 ToSHA1() 方法,對 int 類別擴充 Square() 與 Cube() 方法。

   1:  namespace MyExtensionMethods
   2:  {
   3:      /// <summary>
   4:      /// 字串型態的 Extension Methods
   5:      /// </summary>
   6:      public static class StirngExtensionMethods
   7:      {
   8:          /// <summary>
   9:          /// 取得 MD5 加密字串
  10:          /// </summary>
  11:          public static string ToMD5(this string source)
  12:          {
  13:              MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  14:              byte[] bytValue = Encoding.UTF8.GetBytes(source);
  15:              byte[] bytHash = md5.ComputeHash(bytValue);
  16:              md5.Clear();
  17:   
  18:              StringBuilder sb = new StringBuilder();
  19:              for (int i = 0; i < bytHash.Length; i++)
  20:              {
  21:                  sb.Append(bytHash[i].ToString("X").PadLeft(2, '0'));
  22:              }
  23:   
  24:              return sb.ToString().ToLower();
  25:          }
  26:   
  27:          /// <summary>
  28:          /// 取得 SHA-1 加密字串
  29:          /// </summary>
  30:          public static string ToSHA1(this string source)
  31:          {
  32:              SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
  33:              byte[] bytValue = Encoding.UTF8.GetBytes(source);
  34:              byte[] bytHash = sha1.ComputeHash(bytValue);
  35:              sha1.Clear();
  36:   
  37:              StringBuilder sb = new StringBuilder();
  38:              for (int i = 0; i < bytHash.Length; i++)
  39:              {
  40:                  sb.Append(bytHash[i].ToString("X").PadLeft(2, '0'));
  41:              }
  42:   
  43:              return sb.ToString().ToLower();
  44:          }
  45:      }
  46:   
  47:      /// <summary>
  48:      /// 整數型態的 Extension Methods
  49:      /// </summary>
  50:      public static class IntExtensionMethods
  51:      {
  52:          /// <summary>
  53:          /// 取得平方值
  54:          /// </summary>
  55:          public static int Square(this int i)
  56:          {
  57:              return i * i;
  58:          }
  59:   
  60:          /// <summary>
  61:          /// 取得立方值
  62:          /// </summary>
  63:          public static int Cube(this int i)
  64:          {
  65:              return i * i * i;
  66:          }
  67:      }
  68:  }

 

 

 

使用方式:引入 MyEctensionMethods 的命名空間即可 using MyEctensionMethods;,對於相關擴充 string 與 int 類別,intellisense 就會自洞代初剛剛實作 string 類別的 ToMD5() 方法與 ToSHA1() 方法,對 int 類別擴充 Square() 與 Cube() 方法。

ExtensionMethods_001

ExtensionMethods_002 

   1:  string s = "測試";
   2:  string msg = string.Format("{0}, MD5: {1}, SHA1: {2}", s, s.ToMD5(), s.ToSHA1());
   3:  Console.Write(msg);
   4:   
   5:  Console.WriteLine();
   6:   
   7:  int i = 10;
   8:  string outString = string.Format("{0}, {0}^2: {1}, {0}^3: {2}", i, i.Square(), i.Cube());
   9:  Console.Write(outString);

 

 

 

輸出:

測試, MD5: b7dfcd49856b273e35b9504a921c8078, SHA1: 8b2da228cf9412ae0cc10113c08c779821bbc19d
10, 10^2: 100, 10^3: 1000