2010-10-31 37 views
0

嗨我試圖達到這個目標,但就我而言,我什麼都沒有。 我想添加一些靜態方法的字符串類型,這將返回新的更改字符串。我有:使用系統;如何在Asp.net MVC 2中爲字符串類型創建擴展方法?

using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Security.Cryptography; 
using System.Text; 
namespace TestProject.Models 
{ 
    public static class Extension 
    { 
     public static string md5(this string input) 
     { 

      MD5 HashAlgorithm = new MD5CryptoServiceProvider(); 
      Byte[] InputsBytes = Encoding.UTF8.GetBytes(input3); 
      Byte[] HashedInput = HashAlgorithm.ComputeHash(InputsBytes); 
      return BitConverter.ToString(HashedInput); 
     } 
    } 
} 

老實說,我只是不知道它應該在哪裏。我把它放在我的模型目錄中,但我很確定它是錯誤的。它應該在哪裏?那麼呢?我想以這種方式來使用它:

string hashedString = String.md5(input); 
+0

輸入3要輸入正確的? – 2010-10-31 20:50:29

+0

是的,因爲這是因爲我簡化了我的代碼在這裏的文本框。 – TrN 2010-10-31 21:04:37

回答

0

試試這個:string hashedString = input.md5();

擴展方法是編譯器的伎倆實際通話僅僅是一個正常的靜態方法調用。

編譯器只是原來的代碼爲:

string hashedString = TestProject.Models.Extentions.md5(input)

+0

它有幫助! ;)但爲什麼如果我把它當作一個靜態方法來解決呢?是擴展功能嗎? – TrN 2010-10-31 20:58:13

+0

該方法是靜態的,因爲您不需要Extension類的對象來使用它。 – 2010-10-31 21:13:59

+0

對,現在很明顯。 thx尋求幫助和解釋;) – TrN 2010-10-31 21:24:33

相關問題