【发布时间】:2019-11-12 19:52:51
【问题描述】:
我想使用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
有没有可能,怎么做?
【问题讨论】:
-
为什么不
using (var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider()) {...}?您所要做的就是输入全名 -System.Security.Cryptography.MD5CryptoServiceProvider- 一次。 -
不确定你想要什么,但也许你可以将你的课程分成几个文件(每个使用
partial class),然后在每个文件中你可以使用自己的一组usings跨度> -
不是您要查找的内容,但您可以使用完全限定名称,例如“new System.Security.Cryptography.MD5CryptoServiceProvider()”,以获得类似的效果。
-
@DmitryBychenko 如果我会这样做,我将不必在程序顶部写(
using system所在的位置)using System.Security.Cryptography;? -
@avivgood2: 是的,如果你输入
new System.Security.Cryptography.MD5CryptoServiceProvider()你不必写using System.Security.Cryptography;
标签: c# namespaces using