Bug 12468 - Namespace problem using .Android.
Summary: Namespace problem using .Android.
Status: RESOLVED INVALID
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.6.x
Hardware: All Mac OS
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2013-05-30 05:46 UTC by Wille
Modified: 2013-05-30 10:10 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED INVALID

Description Wille 2013-05-30 05:46:54 UTC
I got a problem, when I wanted to create a Spinner, with this Layout: Android.Resource.Layout.SimpleSpinnerItem

google led me to this link:

http://stackoverflow.com/questions/12607111/how-to-get-google-provided-android-resource-layout-references-in-my-mono-for-and

This describes the bug very well. 
Having an application namespace containing .Android. causes that Android.Resource.Layout.* cannot be found.
Changing the application namespace in a way, that Android is substring fixes the problem, but the bug should defenitly be fixed somehow in Mono for Android.
Comment 1 Jonathan Pryor 2013-05-30 10:10:59 UTC
This is a C# language feature, and is behaving as per the C# language specification.

Consider this example:

  using System;

  namespace Example {
    namespace System {
    }

    class Bad : System.Object {
    }
  }

The above fails to compile:

> ns.cs(8,24): error CS0234: The type or namespace name `Object' does not exist in 
> the namespace `Example.System'. Are you missing an assembly reference?

This is true for both .NET CSC and Mono's mcs compilers.

Why? See §10.8 Namespace and type names, pages 100-102.

    http://www.ecma-international.org/publications/standards/Ecma-334.htm 

> * Otherwise, the namespace-or-type-name is of the form N.I or of the form N.I<A1, ..., AK>.
>   N is first resolved as a namespace-or-type-name.

In this case we're processing `System.Object`, which is of the form `N.I`. So we need to first resolve `System`:

> * Otherwise, if the namespace-or-type-name is of the form I or of the form I<A1, ..., AK>:
> ...
>   * Otherwise, for each namespace N, starting with the namespace in which the
>     namespace-or-type-name occurs, continuing with each enclosing namespace
>      (if any), and ending with the global namespace, the following steps are evaluated
>      until an entity is located:
> ...

This resolves the token `System` to `Example.System`. Now that `System` is resolved the compiler attempts to resolve `System.Object`, i.e. the fully qualified name of `Example.System.Object`. This type does not exist, and we get an error.

The fix for the above sample? Use `global::`:

    class Bad : global::System.Object {
    }

The same is true for your Android code; if you happen to be within an `Example.Android` namespace and you need to use the Android.Resource.Layout type, then use global::Android.Resource.Layout.

Or you can use a `using-alias`, which is resolved at the point of declaration, allowing:

  using System;
  using MyObject = System.Object;

  namespace Example {
    namespace System {
    }

    class Bad : MyObject {
    }
  }