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, IDynamicInterfaceImplementationRemarks
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 the prohibitive computational cost. It tries to analyze only 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. Alternatively, you can apply
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 to 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 the 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 the aspect to use arbitrary calls to property getters to suppress false positives, set
the PreventFalsePositives property to true.
The aspect does not raise notifications immediately when a field changes. It buffers them per thread and raises them, on the thread that made the change, when the control flow exits the object, that is, when the outermost call to a method of the object returns. This ensures that clients observe the object in a consistent state, and avoids notifying the same property several times during a single call. To control the buffer, use the NotifyPropertyChangedServices class.
Constructors
| Name | Description |
|---|---|
| NotifyPropertyChangedAttribute() | Initializes a new instance of the NotifyPropertyChangedAttribute class. |
| NotifyPropertyChangedAttribute(PortableFormatterConstructorContext) |
Properties
| Name | Description |
|---|---|
| ExcludeExplicitProperties | Gets or sets a value indicating whether the aspect should instrument only automatic properties and ignore all others. This mode allows you to use only the abilities of PostSharp Essentials, and prevents the aspect from consuming premium features. |
| PreventFalsePositives | Gets or sets a value indicating whether the aspect should make 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 | Gets or sets how the PropertyChanged event keeps 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. |
| OnCloned(ICloneAwareAspect) | Method called after an object enhanced by the current aspect has been
cloned using MemberwiseClone(). The |