Add the ability for an attribute to "preserve" the property it is applied to
- Dominant language
- C#
- Stars
- 392
- Forks
- 128
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 2
Description
Similar to https://github.com/mono/linker/issues/1126. Once the `ValidationAttribute`s are being preserved, we need to preserve the property "getters" for any property marked with a ValidationAttribute.
`ComponentModel.DataAnnotations` will attempt to get the value of any property marked with a `ValidationAttribute`. Today, the getters are being stripped if they are not accessed statically.
See https://github.com/dotnet/runtime/blob/4f9ae42d861fcb4be2fcd5d3d55d5f227d30e723/src/libraries/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/Validator.cs#L515-L518.
We will need a way to preserve the property getters, or else this code will fail at runtime with:
```
Unhandled exception. System.ArgumentException: Property Get method was not found.
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
at System.ComponentModel.DataAnnotations.Validator.GetPropertyValues(Object instance, ValidationContext validationContext)
at System.ComponentModel.DataAnnotations.Validator.GetObjectPropertyValidationErrors(Object instance, ValidationContext validationContext, Boolean validateAllProperties, Boolean breakOnFirstError)
at System.ComponentModel.DataAnnotations.Validator.GetObjectValidationErrors(Object instance, ValidationContext validationContext, Boolean validateAllProperties, Boolean breakOnFirstError)
at System.ComponentModel.DataAnnotations.Validator.TryValidateObject(Object instance, ValidationContext validationContext, ICollection`1 validationResults, Boolean validateAllProperties)
```
cc @vitek-karas
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.