CommunityToolkit / CommunityToolkit/dotnet
Enable other classes that already implement the `INotifyPropertyChanged` to leverage Source Generator Attributes
- Lingua principale
- C#
- Stelle
- 3.8k
- Fork
- 400
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
### Overview
For instance, I already have a class I'm using which inherits from `INotifyPropertyChanged`. In my scenario, it's `ObservableCollection`, so I can't modify it as it's in the BCL.
```cs
public class ObservableCollection : Collection, INotifyCollectionChanged, INotifyPropertyChanged
```
It already has an `OnPropertyChanged` helper and `PropertyChanged` event:
```cs
protected virtual event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChanged?.Invoke(this, e);
}
```
It would be great if I could still use some or all of the MVVM attributes for source generators like `[ObservableProperty]` when inheriting from these classes in my own class.
### API breakdown
N/A uses existing attributes.
### Usage example
For instance, I have a class already inheriting from this:
```cs
public partial class MyClass : ObservableCollection
{
// Since my parent class already inherits from INotifyPropertyChanged (and already has a OnPropertyChanged method), just use those...
[ObservableProperty]
private bool _isModified;
}
```
Worst case, let me have to re-specify that I want whatever's needed to polyfill, either with `INotifyPropertyChanged` or `ObservableObject`:
```cs
[INotifyPropertyChanged]
public partial class MyClass : ObservableCollection
{
[ObservableProperty]
private bool _isModified;
partial OnIsModifiedChanged(bool value)
{
// Yay, I could also do this now...
}
}
```
Basically the existing generator in this case would spit out all the same stuff except what's already implemented by the type (in the case of `ObservableCollection` it'd be the following:
```cs
partial class MyClass : global::System.ComponentModel.INotifyPropertyChanged
{
//// The Event and the event args OnPropertyChanged are already implemented, so skip adding them
///
/// Raises the event.
///
/// (optional) The name of the property that changed.
[global::System.CodeDom.Compiler.GeneratedCode("CommunityToolkit.Mvvm.SourceGenerators.INotifyPropertyChangedGenerator", "8.1.0.0")]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
protected void OnPropertyChanged([global::System.Runtime.CompilerServices.CallerMemberName] string? propertyName = null)
{
OnPropertyChanged(new global::System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
//// All the SetProperty helpers, TaskNotifier, etc...
```
If there's any conflict for some particular type, then raise an analytic warning/error.
### Breaking change?
No
### Alternatives
Can't use MVVM Toolkit generators or helpers? 😥
### Additional context
Don't know if I know enough to help here. Had a hard time trying to understand how the existing code was generated with the new fanciness...
### Help us help you
Yes, if someone can help (I'm not sure I understand where the current `INotifyPropertyChanged` generator is and how it works. Nor do I know if I know how to detect if something inherits or has the required methods to skip outputting them... but beyond that, I'd be willing to assist giving it a go if pointed in the right directions.
Guida per i contributori
Apri la guida per i contributori
Direzione di ricerca
Locate the existing INotifyPropertyChanged source generator entry point and its tests; read how it determines generated members and handles inherited types. Reproduce the ObservableCollection/INotifyPropertyChanged case, then define tests for [ObservableProperty] and the optional [INotifyPropertyChanged] path, including diagnostics for conflicts. Done means generated members reuse compatible inherited APIs without duplicate declarations.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- csharp
- Ambito
- developer-experience, tooling
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100