C#:导入 "local" 对库的引用?

C#: import "local" reference to library?

这应该是一个很简单的问题,但要找到答案却相当困难。

在 Python 中,您可以通过两种(主要)方式导入库:

import myLibrary
thisObject = myLibrary.myObject()

或:

from myLibrary import myObject
thisObject = myObject

现在,在 C# 中,您通常使用 using 来完成或多或少相同的事情:

using System;

但是,请注意 Python 中的第二个选项让我们编写的代码不包含对相关对象的完整命名空间路径引用。

一旦您开始深入研究 .NET,就会发现一些类被埋得很深。这可以生成一些 "messy" 代码。

using System;
System.Security.Cryptography.X509Certificates.PublicKey k1 = new System.Security.Cryptography.X509Certificates.PublicKey(...);
System.Security.Cryptography.X509Certificates.PublicKey k2 = new System.Security.Cryptography.X509Certificates.PublicKey(...);

我希望我能做的是这样的(伪代码):

using PublicKey from System.Security.Cryptography.X509Certificates;
PublicKey k1 = new PublicKey(...);
...

现在,我了解到导入单个特定对象可能不可行。但至少,我们可以在本地导入整个库吗?例如,System.Security.Cryptography 是一个集合。所以:

using Cryptography from System.Security;
Cryptography.X509Certificates.PublicKey k1 = new Cryptography.X509Certificates.PublicKey(...);
...

最后,这可能是不可能的,但是我们可以做类似于 Python 的任何事情吗?我们可以在其中为导入分配一个更本地化的名称?

using Cryptography from System.Security as crypt;
crypt.AESCryptoServiceProvider acsp = new crypt.AESCryptoServiceProvider();

真正的目标只是让代码至少具有更高的可读性(和更少的重复性)。

感谢任何建议!

在 C# 中,单个 类 或整个命名空间是可能的。参见 MSDN

 using PublicKey = System.Security.Cryptography.X509Certificates;
 PublicKey k1 = new PublicKey(...);

 using crypt = System.Security.Cryptography;
 crypt.AESCryptoServiceProvider acsp = new crypt.AESCryptoServiceProvider();