Pre- Posthook auto generated getter and setter functions #2502
Replies: 6 comments
-
This sounds like something that could be accomplished using source generators (dotnet/roslyn#5561), which are planned for the next version of C# after C# 7.0. |
Beta Was this translation helpful? Give feedback.
-
Pretty sure that's one of the prototypical examples of usage for source generators. Effectively you'd have the following: [GenerateNotifyPropertyChanged] // or some such attribute
public partial class Foo {
public string PropertyA { get; set; }
public string PropertyB { get; set; }
} and the source generator could construct the following and add it into the compilation automatically: public partial class Foo : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
replace string PropertyA {
get { return original; }
set {
original = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PropertyA)));
}
}
replace string PropertyB {
get { return original; }
set {
original = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PropertyB)));
}
}
} The exact implementation would be up to the generator. |
Beta Was this translation helpful? Give feedback.
-
Wow that sounds great, i am looking forward to it.. It´s much more generic! |
Beta Was this translation helpful? Give feedback.
-
Label New Language Feature - Replace/Original? |
Beta Was this translation helpful? Give feedback.
-
Any progress on this? 🤔 |
Beta Was this translation helpful? Give feedback.
-
Nobody from the language team has championed to take this proposal through language design. Without a champion there will not be progress on this. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Could we have something like
`
[CompilerServices.PostSetHook(MyClass.PostSet)]
class MyClass : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
string PropertyA {get; set;}
string PropertyB {get; set;}
internal static void PostSet(object instance, string propertyName, object value) {
instance.PropertyChanged?.Invoke(instance, new PropertyChangedEventArgs(propertyName));
}
}
`
The compiler should inline the postSet function to every autogenerated Property Setter. This should be availiable for set get add remove as pre and post actions.. So one does not have to implement it for each and every property if they all do the same!
Beta Was this translation helpful? Give feedback.
All reactions