編集

Share via


ObservableRecipientAttribute Class

Definition

An attribute that indicates that a given type should have all the members from ObservableRecipient generated into it. This can be useful when you want the same functionality from ObservableRecipient into a class that already inherits from another one (since C# doesn't support multiple inheritance). This attribute will trigger the source generator to just create the same APIs directly into the decorated class. For instance, this attribute can be used to easily combine the functionality from both ObservableValidator and ObservableRecipient, by using ObservableValidator as the base class and adding this attribute to the declared type.

This attribute can be used as follows:

[ObservableRecipient]
partial class MyViewModel : ObservableValidator
{
    // Other members here...
}
And with this, the same APIs from ObservableRecipient will be available on this type as well.

To avoid conflicts with other APIs in types where the new members are being generated, constructors are only generated when the annotated type doesn't have any explicit constructors being declared. If that is the case, the same constructors from ObservableRecipient are emitted, with the accessibility adapted to that of the annotated type. Otherwise, they are skipped, so the type being annotated has the respondibility of properly initializing the Messenger property. Additionally, if the annotated type inherits from ObservableValidator, the SetProperty<T>(T, T, Boolean, String) overloads will be skipped as well, as they would conflict with the SetProperty<T>(T, T, Boolean, String) methods.

[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false, Inherited=false)]
public sealed class ObservableRecipientAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false, Inherited=false)>]
type ObservableRecipientAttribute = class
    inherit Attribute
Public NotInheritable Class ObservableRecipientAttribute
Inherits Attribute
Inheritance
ObservableRecipientAttribute
Attributes

Remarks

In order to work, ObservableRecipientAttribute needs to be applied to a type that inherits from ObservableObject (either directly or indirectly), or to one decorated with ObservableObjectAttribute. This is because the ObservableRecipient methods rely on some of the inherited members to work. If this condition is not met, the code will fail to build.

Constructors

ObservableRecipientAttribute()

Applies to