我正在处理一个网页,该网页使用两个具有 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/