Custom attribute that, when applied on a type, implements the INotifyPropertyChanged interface for this class and all children classes.
Inheritance
Implements
Namespace: PostSharp.Patterns.Model
Assembly: PostSharp.Patterns.Model.dll
Syntax
[MulticastAttributeUsage(MulticastTargets.Class, Inheritance = MulticastInheritance.Strict, PersistMetaData = true, AllowMultiple = false, TargetTypeAttributes = MulticastAttributes.UserGenerated)]
[ProvideAspectRole("DataBinding")]
[AspectRoleDependency(AspectDependencyAction.Order, AspectDependencyPosition.After, "Tracing")]
[AspectRoleDependency(AspectDependencyAction.Order, AspectDependencyPosition.After, "Threading")]
[AspectRoleDependency(AspectDependencyAction.Order, AspectDependencyPosition.After, "Validation")]
[AspectTypeDependency(AspectDependencyAction.Order, AspectDependencyPosition.After, typeof(AggregatableAttribute))]
[AspectRoleDependency(AspectDependencyAction.Order, AspectDependencyPosition.After, "Caching")]
[AspectRoleDependency(AspectDependencyAction.Order, AspectDependencyPosition.Before, "CacheInvalidation")]
[Metric("UsedFeatures", "PostSharp.Patterns.Model.NotifyPropertyChanged")]
[IntroduceInterface(typeof(INotifyPropertyChanged), OverrideAction = InterfaceOverrideAction.Ignore, AncestorOverrideAction = InterfaceOverrideAction.Ignore)]
[IntroduceInterface(typeof(INotifyChildPropertyChanged), OverrideAction = InterfaceOverrideAction.Ignore, AncestorOverrideAction = InterfaceOverrideAction.Ignore, LinesOfCodeAvoided = 0)]
[IntroduceInterface(typeof(IQueryInterface), OverrideAction = InterfaceOverrideAction.Ignore, AncestorOverrideAction = InterfaceOverrideAction.Ignore)]
[Serializer(typeof(NotifyPropertyChangedAttribute.Serializer))]
public sealed class NotifyPropertyChangedAttribute : InstanceLevelAspect, ITypeLevelAspect, ITypeLevelAspectBuildSemantics, IAspectBuildSemantics, IValidableAnnotation, ICloneAwareAspect, IInstanceScopedAspect, ISerializationCallback, INotifyPropertyChanged, IAdviceProvider, IAspect, IQueryInterface, IDynamicInterfaceImplementation
Remarks
The aspect analyzes dependencies between property getters and fields. The aspect understands indirect dependencies. For instance, a property getter may depend on several other methods or property getters, which in turn can depend on other methods or fields. The algorithm is not able to analyze calls to virtual methods or delegates (because of prohibitive computational cost), and will only try to analyze methods in the current class.
The aspect will emit a build-time error if it is not able to understand the code of the target class. In this case, you can exclude the property from
automatic change notification by applying the IgnoreAutoChangeNotificationAttribute custom attribute to the problematic property, or
SafeForDependencyAnalysisAttribute to ignore the warnings of the dependency analysis engine. You will then need to specify dependencies explicitly using
the Depends class. You can also mark a helper method with the PureAttribute to make it safe from use in a property getter.
For instance, the string.Format
method is pure.
By default, the NotifyPropertyChangedAttribute aspect holds weak references to the client delegates. This behavior prevents memory leaks if client delegates are not properly removed before client objects are disposed. To cause the aspect to store strong references, set WeakEventStrategy property to AlwaysStrong when applying the aspect.
Target classes can customize the NotifyPropertyChangedAttribute aspect by implementing the OnPropertyChanged(string)
method manually. If
this method is implemented, it must be protected
or public
.
By default, the NotifyPropertyChangedAttribute aspect does not call property getters by itself in order to avoid unexpected side-effects. However,
this means that there are false positive notifications of property change. To allow aspect to use arbitrary calls to property getters to suppress false positives, set
the PreventFalsePositives property to true
.
Constructors
Name | Description |
---|---|
NotifyPropertyChangedAttribute() | Initializes a new instance of the NotifyPropertyChangedAttribute class that stores strong references to the client delegates. |
NotifyPropertyChangedAttribute(PortableFormatterConstructorContext) |
Properties
Name | Description |
---|---|
ExcludeExplicitProperties | Determines whether the aspect should instrument only automatic properties and ignore any other. This mode allows to use only the abilities of PostSharp Essentials, and prevent the aspect from consuming premium features. |
PreventFalsePositives | Determines whether the aspect should do additional efforts to prevent the PropertyChanged event from being
raised when the value itself did not change. The default value of this property is |
WeakEventStrategy | Determines how the PropertyChanged event behaves in terms of keeping weak or strong references. |
Methods
Name | Description |
---|---|
CompileTimeInitialize(Type, AspectInfo) | Method invoked at build time to initialize the instance fields of the current aspect. This method is invoked before any other build-time method. |
CompileTimeValidate(Type) | Method invoked at build time to ensure that the aspect has been applied to the right target. |
CreateInstance(AdviceArgs) | Creates a new instance of the aspect based on the current instance, serving as a prototype. |
~NotifyPropertyChangedAttribute() | Destructor. |
OnCloned(ICloneAwareAspect) | Method called after the an object enhanced by the current aspect has been
cloned using MemberwiseClone(). The |