.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
For source generators, a lot of times it is needed to expose only the getter of a property and make the setter protected (or even private).
The use case is when a property should only be changed from inside a command and not from the outside.
Currently, adding an [ObservableProperty] attribute, makes the property public get and public set.
API breakdown
I would add an optional parameter to the ObservablePropertyAttribute constructor:
public ObservablePropertyAttribute();
public ObservablePropertyAttribute(PropertyAccess setter);
public ObservablePropertyAttribute(PropertyAccess setter, PropertyAccess getter);
The enum:
// Same order as: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers
public enum PropertyAccess
{
Public = 0,
Private,
Protected,
Internal,
ProtectedInternal,
PrivateProtected,
}
Usage example
[ObservableProperty(setter: PropertyAccess.Protected)]
private int _counter = 0;
will generate something like this:
private int _counter = 0;
public Counter
{
get => _counter;
protected setter => SetProperty(ret _counter, value);
}
Breaking change?
No
Alternatives
The alternative is not use source generators at all. Or expose the setter, knowing that is a design flaw in the ViewModel API.
Overview
For source generators, a lot of times it is needed to expose only the getter of a property and make the setter protected (or even private).
The use case is when a property should only be changed from inside a command and not from the outside.
Currently, adding an
[ObservableProperty]
attribute, makes the propertypublic get
andpublic set
.API breakdown
I would add an optional parameter to the
ObservablePropertyAttribute
constructor:The enum:
Usage example
will generate something like this:
Breaking change?
No
Alternatives
The alternative is not use source generators at all. Or expose the setter, knowing that is a design flaw in the ViewModel API.
Additional context
No response
Help us help you
No, just wanted to propose this