Narrow AdvancedCollectionView trimming warning so that it only triggers in unsafe usages#724
Narrow AdvancedCollectionView trimming warning so that it only triggers in unsafe usages#724sylveon wants to merge 6 commits intoCommunityToolkit:mainfrom
Conversation
Arlodotexe
left a comment
There was a problem hiding this comment.
Looks good, but I'd like explicit approval from @Sergio0694 before we merge
Sergio0694
left a comment
There was a problem hiding this comment.
This is not a good way to fix this. This whole type would need to be reworked to be properly trim-safe. For instance, there's nothing enforcing that the T being used to create a sort descriptor will actually match the source type being used. Additionally annotating a T and then suppressing is not correct, the annotations should rather flow. As a rule of thumb, suppressions are almost always incorrect and should be avoided.
Sergio0694
left a comment
There was a problem hiding this comment.
Better than before but it's not entirely safe 😅
| /// Gets the name of property to sort on | ||
| /// </summary> | ||
| public string PropertyName { get; } | ||
| public virtual string? PropertyName { get; } |
There was a problem hiding this comment.
This is a breaking change technically
| Comparer = comparer ?? ObjectComparer.Instance; | ||
| } | ||
|
|
||
|
|
There was a problem hiding this comment.
Nit: extra blank line
| } | ||
|
|
||
|
|
||
| [UnconditionalSuppressMessage("Trimming", "IL2070:'this' argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to target method. The parameter of method does not have matching annotations.", |
There was a problem hiding this comment.
This is not safe. Someone could derive from SortDescriptor, use the base constructor with no annotations, manually assign or override PropertyName to return a property, and then this would silently suppress a valid warning.
There was a problem hiding this comment.
I went with this design to avoid adding a suppression in SortDescription<T>, but you're right that this opens up unsafe extensibility since the classes aren't sealed. Should I just go for the suppression in SortDescription<T>?
There was a problem hiding this comment.
Yeah I think that would be better, at least it's safe.
| /// A generic version of <see cref="SortDescription"/> which preserves the required metadata for reflection-based sorting. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type to sort</typeparam> | ||
| public class SortDescription< |
| } | ||
|
|
||
|
|
||
| [UnconditionalSuppressMessage("Trimming", "IL2070:'this' argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to target method. The parameter of method does not have matching annotations.", |
There was a problem hiding this comment.
Yeah I think that would be better, at least it's safe.
Fixes
Fixes #723
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Trimming warning triggers on every usage of AdvancedCollectionView
What is the new behavior?
Trimming warning only triggers when you make use of reflection-based sorting.
PR Checklist
Please check if your PR fulfills the following requirements:
Other information