-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix DataGrid native aot crash when sorting. #17248
Conversation
You can test this PR using the following package version. |
|
@cla-avalonia agree |
You can test this PR using the following package version. |
else if (type.IsAssignableTo(typeof(IComparable))) | ||
return Comparer<object>.Create((x, y) => (x as IComparable)!.CompareTo(y)); | ||
else | ||
return Comparer<object>.Create((x, y) => 0); //avoid using reflection to avoid crash on AOT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On NET6_0_OR_GREATER you can check for this property whether reflection is supported: https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.runtimefeature.isdynamiccodesupported?view=net-8.0
On AOT builds IsDynamicCodeSupported returns false, and this fallback would make sense there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way we also won't regress any application that relies on this reflection sorting right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice suggestion. Done.
User can not use sort feature when they does not provide metadata of the type when using aot, Can we do better to avoid user make this kind of mistake? such as add some aot compile warning, or add [DynamicallyAccessedMembersAttribute] or [DynamicDependency] to somewhere? @maxkatz6 do you have any idea on it? |
You can test this PR using the following package version. |
@dameng324 we could generate Ideally, AOT apps would utilize Other than this, any refactorings of DataGrid control are not planned. |
@maxkatz6 Thanks for the info. solving this by xaml compiler will be really nice. |
@maxkatz6 I wonder when will it be published? I didn't see this in the latest 11.2 rc version. |
* Fix DataGrid native aot crash when sorting. * Update DataGridSortDescription.cs * add customType test * does not crash when property type is not sortable. * check `RuntimeFeature.IsDynamicCodeSupported` to fallback to reflection implemention
What does the pull request do?
When sorting datagrid with a non string type field, the application will crash as no metadata of
Comparer<int>
. This PR fix it.Fixed: #14059
What is the current behavior?
When user does not provide the ViewModel meta data (by [DynamicDependcy] or Rd.xml etc), the application does not crash when sorting int field. but the sort function is broken.
When user provide the ViewModel meta data (by [DynamicDependcy] or Rd.xml etc), the application will crash when sorting int field.
What is the updated/expected behavior with this PR?
When user does not provide the ViewModel meta data (by [DynamicDependcy] or Rd.xml etc), the application will behave same as before when sorting.
When user provide the ViewModel meta data (by [DynamicDependcy] or Rd.xml etc), the application will work normal when sorting
int
/int?
/Enum
/Enum?
and other type which implementsIComparable
interface when using aot. Other type which does not implementsIComparable
should not crash, only does nothing when sorting.How was the solution implemented (if it's not obvious)?
Old solution:
My solution:
Checklist
Breaking changes
Obsoletions / Deprecations
Fixed issues
Fixed: #14059