Visual Studio 2015 智能感知和不明确的命名空间

Visual Studio 2015 intellisense and ambiguous namespaces

Xamarin.Auth 项目中断,即源代码编译正常,在另一个 Xamarin.Android 更新后突然中断。

原来问题是在 Mono.Android.dll (Xamarin.Android) 被几个命名空间定义扩展后出现的。

我将问题归结为以下几点:

有一个 dll #1 (Mono.Android.dll):

namespace Xamarin.Android
{
    public class Class1
    {
    }
}

namespace Android.OS
{
    public class Class2
    {
    }
}

有一个 dll #2 (Xamarin.Auth.Android.dll):

namespace Xamarin.Auth
{
    //This does not compile. See the problem description below.
    public class User1 : Android.OS.Class2
    {
    }
}

Intellisense 显示以下问题:

Error CS0234 The type or namespace name 'OS' does not exist in the namespace 'Xamarin.Android' (are you missing an assembly reference?)

这可以通过将后一个名称空间更改为其他名称或使用 global:: 标识符来解决:

namespace SomeOtherNamespace
{
    //This compiles ok.
    public class User1 : Android.OS.Class2
    {
    }
}

namespace Xamarin.Auth
{
    //This compiles ok.
    public class User1 : global::Android.OS.Class2
    {
    }
}

问题是:为什么 intellisense 不警告 Android 命名空间分支在 global::Xamarin.Androidglobal::Android 之间不明确?摆脱它的好方法是什么?始终使用 global:: 命名空间标识符?

您可以使用 global 指令告诉编译器它应该从根开始计算命名空间,否则它会尝试从当前命名空间计算相对命名空间。使用 global 始终使用完全限定的命名空间名称,因此这就是为什么在您为它添加前缀时它会起作用的原因。

另一种选择是使用别名:

using AOS = Android.OS;

namespace Xamarin
{
    public class User1 : AOS.Class2
    {
    }
}