c# - 如何为有限的代码部分使用命名空间

标签 c# namespaces using

我想用namespace System.Security.Cryptography但仅适用于代码的有限部分,因此如果我尝试使用命名空间的 classesfunction超出定义的区域它将不起作用。我期待的结果类似于 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/

相关文章:

xml - WPF - 基于另一个 xmlns 定义一个 xmlns

c# - 在 C# 中维护 "clean"using 指令列表有什么好处?

c# List<Tuple<string, string, int>> group by min max int

c# - 在多个浏览器上并行运行测试

c# - WPF DataGrid 中基于基础数据集(及其类型)的动态 DataGrid 列

xml - 如何消除重复的 XML 命名空间定义?

c# - 跨账户访问 DynamoDb 表 C#

c++ - 为什么 Boost.Hana 使用宏来打开/关闭命名空间?

c# - 在 C# 应用程序中处理位图

android - 如何从接收器调用服务取决于互联网连接与否?