-
-
Notifications
You must be signed in to change notification settings - Fork 39
Description
Is the feature request related to a problem
A potential CS0104 might occur in polyfillings when global using directives are used.
Example:
When consuming both Polyfill (7.33.0+) and either Microsoft.MacCatalyst or Microsoft.iOS:
RequiredMemberAttribute.cs(32,35): Error CS0104 : 'RequiredMemberAttribute' is an ambiguous reference between 'Foundation.RequiredMemberAttribute' and 'System.Runtime.CompilerServices.RequiredMemberAttribute'
Then an ambiguity with Foundation.RequiredMemberAttribute can be caused in the polyfill when the consuming project is also declaring a global using directive:
<ItemGroup>
<Using Include="Foundation" />
</ItemGroup>Minimal Repro:
- new .NET MacOS or iOS project
- add Polyfill (7.33.0+)
- add global using directive
"Foundation"<Using Include="Foundation" />global using Foundation;
- error CS0104
Describe the solution
Disambiguate by avoiding using directives for forwarded types.
Option 1: Fully qualified type names
#else
-using System.Runtime.CompilerServices;
-[assembly: TypeForwardedTo(typeof(RequiredMemberAttribute))]
+[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.CompilerServices.RequiredMemberAttribute))]
#endifDrawback:
Duplicates the namespace in particular for polyfills in the System.Runtime.CompilerServices namespace,
making the source / dependency a bit larger.
Option 2: Global alias
#else
-using System.Runtime.CompilerServices;
-[assembly: TypeForwardedTo(typeof(RequiredMemberAttribute))]
+[assembly: global::System.Runtime.CompilerServices.TypeForwardedTo(typeof(global::System.Runtime.CompilerServices.RequiredMemberAttribute))]
#endifDrawback:
Although being the "safest" way to reference types in auto-generated code,
this makes the source / dependency even larger.
Describe alternatives considered
The alternative is to not consider this (and related) scenarios in Polyfill,
and instead let the consuming project resolve the issue by avoiding or removing problematic global using directives.
E.g.:
<ItemGroup>
<Using Remove="Foundation" />
</ItemGroup>Additional context
This issue may also occur in other places of Polyfill,
but I only considered forwarded types for the scope of this issue,
as this is what we ran into in https://github.com/getsentry/sentry-dotnet.