Open sandboxFocus

Method IntroduceProperty

IntroduceProperty(IAdviser<INamedType>, string, IntroductionScope, OverrideStrategy, Action<IPropertyBuilder>?, object?)

Introduces a property to the target type, or overrides the implementation of an existing one, by specifying a property template. Use the With<TNewDeclaration>(TNewDeclaration) method to apply the advice to a different type than the current one.

Declaration
public static IIntroductionAdviceResult<IProperty> IntroduceProperty(this IAdviser<INamedType> adviser, string template, IntroductionScope scope = IntroductionScope.Default, OverrideStrategy whenExists = OverrideStrategy.Default, Action<IPropertyBuilder>? buildProperty = null, object? tags = null)
Parameters
Type Name Description
IAdviser<INamedType> adviser

An adviser for a named type.

string template

The name of the property in the aspect class that will be used as a template for the new property. This property must be annotated with TemplateAttribute. The type of this property can be either dynamic or any specific type. It is possible to dynamically change the type of the introduced property thanks to the IPropertyBuilder returned by this method.

IntroductionScope scope

Determines the scope (e.g. Instance or Static) of the introduced property. The default scope depends on the scope of the template property. If the template property is static, the introduced property is static. However, if the template property is non-static, then the introduced property copies the scope of the target declaration of the aspect.

OverrideStrategy whenExists

Determines the implementation strategy when a property of the same name is already declared in the target type. The default strategy is to fail with a compile-time error.

Action<IPropertyBuilder> buildProperty

An optional delegate that modifies the IPropertyBuilder representing the introduced property.

object tags

An optional object (typically of anonymous type) passed to the template and accessible via meta.Tags. See Sharing state with adviceSharing state with advice for details.

Returns
Type Description
IIntroductionAdviceResult<IProperty>

An IIntroductionAdviceResult<T> exposing the introduced or overriding IProperty.

See Also

IntroduceProperty(IAdviser<INamedType>, string, string?, string?, IntroductionScope, OverrideStrategy, Action<IPropertyBuilder>?, object?, object?)

Introduces a property to the target type by specifying individual template methods for each accessor (getter and setter).

Declaration
public static IIntroductionAdviceResult<IProperty> IntroduceProperty(this IAdviser<INamedType> adviser, string name, string? getTemplate, string? setTemplate, IntroductionScope scope = IntroductionScope.Default, OverrideStrategy whenExists = OverrideStrategy.Default, Action<IPropertyBuilder>? buildProperty = null, object? args = null, object? tags = null)
Parameters
Type Name Description
IAdviser<INamedType> adviser

An adviser for a named type.

string name

Name of the introduced property.

string getTemplate

The name of the method of the aspect class whose type and implementation will be used as a template for the getter, or null if the introduced property should not have a getter. This method must be annotated with TemplateAttribute. The signature of this method must be T Get() where T is either dynamic or a type compatible with the type of the property.

string setTemplate

The name of the method of the aspect class whose type and implementation will be used as a template for the setter, or null if the introduced property should not have a setter. This method must be annotated with TemplateAttribute. The signature of this method must be void Set(T value) where T is either dynamic or a type compatible with the type of the property.

IntroductionScope scope

Determines the scope (e.g. Instance or Static) of the introduced property. The default scope depends on the scope of the template accessors. If the accessors are static, the introduced property is static. However, if the template accessors are non-static, then the introduced property copies the scope of the target declaration of the aspect.

OverrideStrategy whenExists

Determines the implementation strategy when a property of the same name is already declared in the target type. The default strategy is to fail with a compile-time error.

Action<IPropertyBuilder> buildProperty

An optional delegate that modifies the IPropertyBuilder representing the introduced property.

object args

An optional object (typically of anonymous type) whose properties map to template method parameters or type parameters. See Sharing state with adviceSharing state with advice for details.

object tags

An optional object (typically of anonymous type) passed to the template and accessible via meta.Tags. See Sharing state with adviceSharing state with advice for details.

Returns
Type Description
IIntroductionAdviceResult<IProperty>

An IIntroductionAdviceResult<T> exposing the introduced or overriding IProperty.

Remarks

Unlike the overload that takes a template property, this method allows you to specify separate template methods for the getter and setter, providing more flexibility in property implementation. This is useful when you need different logic for reading and writing the property value.

Use With<TNewDeclaration>(TNewDeclaration) to introduce the property into a different type than the current target.

See Also