You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Context: dc3ccf2
Context: dotnet/maui#12520 (comment)
Context: https://discord.com/channels/732297728826277939/732297837953679412/1062137297438519296
Context: https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0002
Consider this C# fragment:
namespace Android.Content {
public class Intent {
}
}
namespace Microsoft.Maui.Example {
public class Helper {
public static void UseIntent (Android.Content.Intent intent) {}
}
}
Given a C# *namespace_or_type_name* such as `Android.Content.Intent`,
[ECMA 334 §7.8.1][0] will be used to determine what the type refers
to, resolving it to the Assembly-Qualified Name
`Android.Content.Intent, Mono.Android`.
All is good.
However, once dc3ccf2 enters the picture, we now have:
// via dc3ccf2 & _Microsoft.Android.Resource.Designer.dll
namespace Microsoft.Android.Resource.Designer {
public class Resource {
}
}
namespace Android.Content {
public class Intent {
}
}
namespace Microsoft.Maui.Example {
public class Helper {
// CS0234 on the following line:
public static void UseIntent (Android.Content.Intent intent) {}
}
}
…and it fails to build with [error CS0234][1]:
error CS0234: The type or namespace name 'Content' does not exist in the namespace 'Microsoft.Android' (are you missing an assembly reference?)
This only happens if the usage of `Android.Content.Intent` happens
*within* a `Microsoft.`-prefixed namespace. Which is most of MAUI.
The cause of the error is that while attempting to resolve
`Android.Content.Intent`, search starts from the *current* namespace
`Microsoft.Maui.Example`, and the compiler will attempt to resolve
the types:
* `Microsoft.Maui.Example.Android.Content.Intent`
(because we're in the `Microsoft.Maui.Example` namespace.)
* `Microsoft.Maui.Android.Content.Intent`
(because `Microsoft.Maui` is a parent namespace)
* `Microsoft.Android.Content.Intent`
(because `Microsoft` is a parent parent namespace)
These are the *only* types that are resolved, and none of those exist.
This results in the CS0234 error.
There are two workarounds that the "offending" code can employ:
1. Use `global`:
namespace Microsoft.Maui.Example {
public class Helper {
public static void UseIntent (global::Android.Content.Intent intent) {}
}
}
2. Add a `using Android.Content` and use `Intent`
using Android.Content;
namespace Microsoft.Maui.Example {
public class Helper {
public static void UseIntent (Intent intent) {}
}
}
Both of these require changing things *outside* of xamarin-android.
Rephrasing & simplifying: commit dc3ccf2 constitutes an *API break*,
as code which *previously* compiled *no longer compiles*.
Fix this by updating the `<GenerateResourceDesignerAssembly/>` task to
emit types into the `_Microsoft.Android.Resource.Designer` namespace;
note `_` prefix. No One™ should have their code in a `_Microsoft.*`
namespace, so this shouldn't break anybody. Additionally, update
`_Microsoft.Android.Resource.Designer.dll` so that
[`EditorBrowsableAttribute`][2] is placed on all the types, a'la:
namespace _Microsoft.Android.Resource.Designer;
[EditorBrowsable (EditorBrowsableState.Never)]
public partial class Resource {
}
// In App project builds
[EditorBrowsable (EditorBrowsableState.Never)]
internal partial class ResourceConstant {
}
Finally, update the Source Compatibility types so that the `Resource`
type disables the IDE0002 warning:
namespace %CompatibilityNamespace% {
#pragma warning disable IDE0002
public partial class Resource : _Microsoft.Android.Resource.Designer.Resource {
}
#pragma warning restore IDE0002
}
[0]: https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/basic-concepts.md#78-namespace-and-type-names
[1]: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0234
[2]: https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.editorbrowsableattribute?view=net-7.0
[3]: https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0002
0 commit comments