我想用namespace System.Security.Cryptography
但仅适用于代码的有限部分,因此如果我尝试使用命名空间的 classes
或 function
超出定义的区域它将不起作用。我期待的结果类似于 using
声明于 types
, 但与 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);
}
希望你现在得到它:)
关于c# - 如何为有限的代码部分使用命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56847836/