-
Notifications
You must be signed in to change notification settings - Fork 351
Closed
Labels
feature request 📬A request for new changes to improve functionalityA request for new changes to improve functionalitymvvm-toolkit 🧰Issues/PRs for the MVVM ToolkitIssues/PRs for the MVVM Toolkit
Description
Overview
This issue tracks adding support for partial properties to the [ObservableProperty]
generator. This is blocked on dotnet/csharplang#6420, so support for this will have to wait for that first (hopefully in the C# 12 timeframe).
API breakdown
The changes are pretty straightforward: properties will also be allowed on [ObservableProperty]
:
-[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
+[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class ObservablePropertyAttribute : Attribute
{
}
Analyzers will also need to be added to ensure that the annotated properties are partial and valid.
Usage example
Code like this:
[ObservableProperty]
public partial string Name { get; private set; }
Will generate the following:
/// <inheritdoc cref="_itemsSource"/>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.1.0.0")]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public partial string Name
{
get => field;
set
{
if (!global::System.Collections.Generic.EqualityComparer<string>.Default.Equals(field, value))
{
OnNameChanging(value);
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Name);
field = value;
OnItemsSourceChanged(value);
OnNameChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Name);
}
}
}
/// <summary>Executes the logic for when <see cref="Name"/> is changing.</summary>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.1.0.0")]
partial void OnNameChanging(string value);
/// <summary>Executes the logic for when <see cref="Name"/> just changed.</summary>
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "8.1.0.0")]
partial void OnNameChanged(string value);
This will also enable support for declaring custom accessibility modifiers for property accessors.
Progress tracking
- Initial code generation support
- Add analyzer for C# preview
- Add analyzer to use partial properties instead of fields
- Add code fixer to convert fields to partial properties
- Add analyzer for partial property declarations
- Add analyzer for unsupported Roslyn version for properties
- Update all titles/descriptions of existing shared diagnostics
nicjay, vslee, victorperez2911, luislhg, sbaeumlisberger and 38 moremyangelkamikaze, stevemonaco, michael-hawker, HavenDV, dmitriyse and 11 moreMrxx99, TheCodeTraveler and Mertschkmgallahan, zxbmmmmmmmmm, Mrxx99 and Rekkonnect
Metadata
Metadata
Assignees
Labels
feature request 📬A request for new changes to improve functionalityA request for new changes to improve functionalitymvvm-toolkit 🧰Issues/PRs for the MVVM ToolkitIssues/PRs for the MVVM Toolkit
Type
Projects
Status
✅ Done