Implements change notification for properties (ie: INotifyPropertyChanged) using IL weaving and a custom Visual Studio build task.
This is how it works:
The NuGet package now covers .NET framework versions 4.0-4.7.
install-package Mathtone.MIST
. Visual Studio will prompt you to reload the project.Using MIST from Nuget.org
Install-Package Mathtone.MIST
<UsingTask TaskName="Mathtone.MIST.NotificationWeaverBuildTask" AssemblyFile="$(ProjectDir)..\packages\Mathtone.MIST.1.0.1\tools\Mathtone.MIST.Builder.dll" /> <Target Name="AfterBuild"> <NotificationWeaverBuildTask TargetPath="$(TargetPath)" DebugMode="True" /> </Target>
Apply notification attributes
Typical usage scenario:
public class NotifierBase : INotifyPropertyChanged {
[NotifyTarget]
protected void RaisePropertyChanged(string propertyName) {
var method = PropertyChanged;
if (method != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
[Notifier(NotificationMode.Implicit)]
public class ImplicitTestNotifier : NotifierBase {
public string Property1 { get; set; }
public string Property2 { get; set; }
public string Property3 { get; set; }
[SupressNotify]
public string Supressed { get; set; }
[Notify("Prop1And2", "Property1", "Property2")]
public string Prop1And2 { get; set; }
}
[Notifier]
public class ExplicitTestNotifier : NotifierBase {
[Notify]
public string Property1 { get; set; }
[Notify]
public string Property2 { get; set; }
[Notify]
public string Property3 { get; set; }
public string Supressed { get; set; }
[Notify("Prop1And2", "Property1", "Property2")]
public string Prop1And2 { get; set; }
}
If it's all wired up correctly, you should see a message in the build output window: "Lightly Misting: <your project output>" at build time.
OnChange Notification:
Additionally, the MIST utility supports notification only when the value being set changes from a previous value. This is controlled by setting the notifycation style to NotificationStyle.OnChange and this can be applied to the NotifierAttribute as a default, or the NotifyAttribute as an override. This method respects all known conventions (at least those known to me) for the comparing of equality among value & reference types, including overridden "Equals" methods, etc:
[Notifier(NotificationMode.Implicit, NotificationStyle.OnChange)]
public class OnChangeTest : TestNotifierBase
{
public string StringValue { get; set; } = "";
[Notify(NotificationStyle.OnSet)]
public string ExplicitOnSetString { get; set; }
}
Happy coding!