Open jonathanpeppers opened 1 month ago
Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas See info in area-owners.md if you want to be subscribed.
The warning is due to DynamicallyAccessedMembers
on this base class of the property containing type: https://github.com/dotnet/maui/blob/d8552206eb4a7f39bea3ac1c3d71ce9373e88338/src/Controls/src/Core/VisualElement/VisualElement_StyleSheet.cs#L12, which has NonPublicFields
. This annotation says that non-public fields of any derived types might be reflected over, and the warning guards against violating the annotations on the field via reflection.
Here's an example of what the warning guards against:
using System.Reflection;
using System.Diagnostics.CodeAnalysis;
var t = new C().GetType();
AssignField(t, typeof(D));
if (C.P.GetMethods().Length == 0) {
throw new Exception("No methods"); // Will throw in AOT only
}
static void AssignField([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicFields)] Type t, Type value) {
foreach (var field in t.GetFields(BindingFlags.NonPublic | BindingFlags.Static)) {
field.SetValue(null, value);
}
}
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicFields)]
class C {
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
public static Type? P { get; set; }
}
class D {
public static void M() {}
}
Changing it to an explicitly implemented property won't help, it'll just move the warning to the explicit field.
A more precise analysis could potentially get rid of these warnings and instead warn at the GetType
or AssignField
callsite. But the current semantics basically make it illegal to put DynamicallyAccessedMembers
annotations on fields when a base type has DynamicallyAccessedMembers.(Non)PublicFields
. In practice it's probably OK to suppress this if you're reasonably sure that nobody will use reflection to violate the field annotation.
So, I think the issue here is that the warning message could be improved. If the property was mentioned instead of k__BackingField
, I wouldn't have thought it was a bug.
Description
For the property:
We got the trimmer warning:
This was inside a .NET MAUI project on iOS.
We think something like this will workaround this case, but CI is ongoing:
Reproduction Steps
dotnet new maui
Root everything:
dotnet publish -c Release -p:PublishAot=true -f net9.0-ios
Expected behavior
We would not get trimmer warnings for
k__BackingField
.Actual behavior
We get trimmer warnings for
k__BackingField
.Regression?
Not sure
Known Workarounds
I assume we can do:
Configuration
Other information
No response