dotnet
dotnet copied to clipboard
Add Custom Attribute Support for Properties Auto-Generated by [ObservableProperty]
Describe the problem
I propose adding support for custom attributes on properties auto-generated by the [ObservableProperty] in the .NET Community Toolkit. Currently, properties auto-generated using [ObservableProperty] do not support attaching additional attributes directly, limiting their application in scenarios like data validation or UI mapping.
Describe the solution
When using [ObservableProperty], there should be a way to attach additional attributes to the generated properties, rather than manually writing out the full properties. In many cases, we need to attach specific attributes to properties of a data model for data validation, serialization control, or specific UI behaviors. For instance, we might want to use [DataAnnotations] attributes for validating data models, or custom attributes to control how columns are displayed.
Alternatives
No response
Additional info
No response
Help us help you
None
Hello, 'Lwc-LiuWenCheng! Thanks for submitting a new feature request. I've automatically added a vote 👍 reaction to help get things started. Other community members can vote to help us prioritize this feature in the future!
In my experience, doing the following has worked: [property:Required]
Inspecting the generated code, the property inherits the attribute.
You can do that here:
https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/generators/observableproperty#adding-custom-attributes
Closing this, as it's already supported 🙂