如何在 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);
Hi I'm trying to reach this goal but as far as I am, I've got nothing.
I wish to add some static method to string type, which would return new changed string. I've got: using System;
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);
}
}
}
honestly I just don't know where should it be. I put it in my models catalog but I'm pretty sure it's wrong. Where should it be? And what then? I'd like to use it in this way:
string hashedString = String.md5(input);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
string hashedString = input.md5();
扩展方法是编译器技巧,实际调用只是普通的静态方法调用。
编译器只是将代码转换为:
string hashedString = TestProject.Models.Extentions.md5(input)
try this :
string hashedString = input.md5();
Extension methods are compiler trick the actual call is just a normal static method call.
The compiler just turns the code into:
string hashedString = TestProject.Models.Extentions.md5(input)