Asp.net mvc 2 如何在Asp.net MVC 2中为字符串类型创建扩展方法?

Asp.net mvc 2 如何在Asp.net MVC 2中为字符串类型创建扩展方法?,asp.net-mvc-2,extension-methods,Asp.net Mvc 2,Extension Methods,嗨,我正在努力达到这个目标,但就我而言,我什么都没有。 我希望向字符串类型添加一些静态方法,它将返回新的更改字符串。我有:使用系统 using System.Collections.Generic; using System.Linq; using System.Web; using System.Security.Cryptography; using System.Text; namespace TestProject.Models { public static class Ext

嗨,我正在努力达到这个目标,但就我而言,我什么都没有。 我希望向字符串类型添加一些静态方法,它将返回新的更改字符串。我有:使用系统

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); 
试试这个:string hashedString=input.md5

扩展方法是编译器技巧,实际调用只是一个普通的静态方法调用

编译器只是将代码转换为:


string hashedString=TestProject.Models.extensions.md5input

input3应该是正确的输入吗?是的,sry,因为我在textBox中简化了我的代码。这很有帮助;但如果我把它当作一个静态方法,为什么会这样呢?它是扩展的特性吗?该方法是静态的,因为您不需要扩展类的对象来使用它。