Open sandboxFocus

Class ImportMemberAttribute

Custom attribute that, when applied to an instance field of an aspect class, determines that this field should be bound to a method, event or property of the target class of this aspect. Valid types for fields are a concrete Delegate (to bind to a method), Property<TValue> or Property<TValue, TIndex> (to bind to a property), or Event<TDelegate> (to bind to an event).

Inheritance
ImportMemberAttribute
Namespace: PostSharp.Aspects.Advices
Assembly: PostSharp.dll
Syntax
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public sealed class ImportMemberAttribute : Advice
Remarks

Fields annotated with the ImportMemberAttribute custom attribute must be public and must not be read-only.

At runtime, these fields are assigned to a delegate (in case of method binding) or a pair of delegates (in case of event or property binding) that can be used to invoke the imported member.

When the IsRequired property is set to true and the target type does not contain a member of the required name and signature, a build error is raised.

When multiple member names are specified, the first existing member satisfying all conditions is used.

When the same aspect also introduces or overrides the member, Order determines whether the field is bound to the member as it was before or after the introductions of the aspect.

The fields are bound when the aspects of the target object are initialized, normally in the constructor. When objects are created without calling a constructor, for example by a custom deserializer, initialize the aspects manually as described in Coping with Custom Object Serializers.

To choose the imported members at build time, or to import several fields or properties into a single aspect field, implement IAdviceProvider and return ImportLocationAdviceInstance or ImportMethodAdviceInstance objects instead.

Constructors

Name Description
ImportMemberAttribute(string)

Initializes a new instance of the ImportMemberAttribute class and specifies a single name for the member to import.

ImportMemberAttribute(params string[])

Initializes a new instance of the ImportMemberAttribute class and specifies several possible names for the member to import.

Properties

Name Description
IsRequired

Gets or sets a value indicating whether a build-time error must be issued if the member to be imported is absent. If false, the binding field is null when the imported member is absent.

MemberNames

Gets the possible names of the imported member, in order of precedence.

Order

Gets or sets the moment when the member should be imported: either before (BeforeIntroductions) or after (AfterIntroductions) members have been introduced by the current aspect. The default is AfterIntroductions.

See Also