我想使用 namespace System.Security.Cryptography
,但仅适用于代码的有限部分,以便我尝试在定义的区域中使用命名空间的 classes
或 function
。我期望的结果与types
中的using
语句类似,但是使用namespaces
。
这是一个示例代码,可以展示我想要的内容:
using(System.Security.Cryptography;){
// namespace can be used from now on
using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
{
UTF8Encoding utf8 =new UTF8Encoding();
byte[] data = md5.ComputeHash(utf8.GetBytes(input));
return Convert.ToBase64String(data);
}
}
//now namespace can not be used- error if you are trying to use it
可以做以及如何做?
将其放在使用中,或者只是使用例如:
System.Security.Cryptography.MD5CryptoServiceProvider
那么不需要使用。
我的意思是:
using (System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider())
{
System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding();
byte[] data = md5.ComputeHash(utf8.GetBytes(input));
return Convert.ToBase64String(data);
}
希望您现在得到它:(
我建议使用完整的合格名称 System.Security.Cryptography.MD5CryptoServiceProvider
代替 using
缩短名称( MD5CryptoServiceProvider
(:
// var - let compiler derive the type
using (var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider())
{
UTF8Encoding utf8 = new UTF8Encoding();
byte[] data = md5.ComputeHash(utf8.GetBytes(input));
return Convert.ToBase64String(data);
}
如果这样做,您不必完全放置using System.Security.Cryptography;