.net - 当类名不明确时可以指定优先命名空间吗?

标签 .net namespaces

我正在处理一个网页,该网页使用两个具有 Label 类的命名空间:

  • Micofosoft.SharePoint.Taxonomy.Label
  • System.Web.UI.WebControls.Label

我有一些类似于下面的代码:

Label lbl_Name = new Label();

我被迫更改为:

System.Web.UI.WebControls.Label lbl_Name = new System.Web.UI.WebControls.Label();

因为我得到了错误:

'Label' is an ambiguous reference between 'System.Web.UI.WebControls.Label' and 'Microsoft.SharePoint.Taxonomy.Label'

我知道我可以“别名”(不确定这是否是正确的术语)命名空间,这样我就不必像这样在我的类(class)面前拥有整个东西:

using UI = System.Web.UI.WebControls;

但我想知道是否有任何方式可以指定在发生冲突时我希望始终使用一个 namespace 。如果有冲突,是否有某种方法可以指定一个命名空间应排在最前面?

最佳答案

您可以使用 using 声明为单个文件中的类型创建别名:

using SharePointLabel = Microsoft.SharePoint.Taxonomy.Label;
using WebControlLabel = System.Web.UI.WebControls.Label;

然后:

WebControlLabel lbl_Name = new WebControlLabel();  

关于.net - 当类名不明确时可以指定优先命名空间吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8797235/

相关文章:

c# - 使用和配置来自 .Net C# 控制台应用程序客户端的 Graphql 请求

c# - 丢失的线程会怎样?

c++ - 大型图书馆中命名空间的使用和滥用

r - 将包命名空间导入默认命名空间

c# - 命名空间冲突

c# - 禁用计时器 .. 为什么我不能?

c# - 如何访问 Windows.Storage 命名空间?

c# - 当另一个应用程序进程被触发时,我需要激活我的 only_one_instance wpf 应用程序的主窗口吗?

php - 使用命名空间时找不到类

php - 在 PHP 中使用命名空间解析 XML 数据