Skip to content

Trim warnings when targeting .NET 9 #1549

@martincostello

Description

@martincostello

Using the packages from the latest CI build in an application using Humanizer that I'm trying to get published using native AoT, there's a single remaining warning when targeting net9.0:

/_/src/Humanizer/EnumCache.cs(77): Trim analysis error IL2072: Humanizer.EnumCache`1.TryGetDescription(MemberInfo,String&): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.NonPublicProperties' in call to 'System.Reflection.RuntimeReflectionExtensions.GetRuntimeProperties(Type)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

This appears to correlate to this line of code:

foreach (var property in attrType.GetRuntimeProperties())

I'm not sure what the intended fix for this scenario would be.

I took a look at the code locally, updating the .NET 9 RC2 SDK and adding a net9.0 target and the warning, plus an additional one appear, so these must be due to new annotations in the .NET 9 release.

1>C:\Coding\Humanizr\Humanizer\src\Humanizer\EnumDehumanizeExtensions.cs(30,53,31,72): error IL2026: Using member 'Humanizer.EnumDehumanizeExtensions.DehumanizeTo(String, Type, OnNoMatch)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. The native code for the target enumeration might not be available at runtime.
1>C:\Coding\Humanizr\Humanizer\src\Humanizer\EnumCache.cs(77,38,77,69): error IL2072: 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.NonPublicProperties' in call to 'System.Reflection.RuntimeReflectionExtensions.GetRuntimeProperties(Type)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions