Open sandboxFocus

Method AddContract

AddContract(IAdviser<IParameter>, string, ContractDirection, object?, object?)

Adds a contract to a parameter. Contracts are usually used to validate parameters (pre- or post-conditions) or to normalize their value (null-to-empty, trimming, normalizing case, ...). Use the With<TNewDeclaration>(TNewDeclaration) method to apply the advice to another declaration than the current one.

Declaration
public static IAddContractAdviceResult<IParameter> AddContract(this IAdviser<IParameter> adviser, string template, ContractDirection direction = ContractDirection.Default, object? args = null, object? tags = null)
Parameters
Type Name Description
IAdviser<IParameter> adviser

An adviser for a parameter.

string template

The name of the template method. This method must have a single run-time parameter named value, and be annotated with the TemplateAttribute custom attribute.

ContractDirection direction

Direction of the data flow to which the contract should apply. See ContractDirection for details.

object args

An object (typically of anonymous type) whose properties map to parameters or type parameters of the template.

object tags

An optional opaque object of anonymous type passed to templates and exposed under the Tags property of the meta API.

Returns
Type Description
IAddContractAdviceResult<IParameter>

AddContract(IAdviser<IFieldOrPropertyOrIndexer>, string, ContractDirection, object?, object?)

Adds a contract to a field, property or indexer. Contracts are usually used to validate the value assigned to fields properties or indexers or to normalize their value (null-to-empty, trimming, normalizing case, ...) before assignment. Alternatively, a contract can be used to validate the value returned by a property or indexer, in which case the direction parameter should be set to Output. Use the With<TNewDeclaration>(TNewDeclaration) method to apply the advice to another declaration than the current one.

Declaration
public static IAddContractAdviceResult<IFieldOrPropertyOrIndexer> AddContract(this IAdviser<IFieldOrPropertyOrIndexer> adviser, string template, ContractDirection direction = ContractDirection.Default, object? args = null, object? tags = null)
Parameters
Type Name Description
IAdviser<IFieldOrPropertyOrIndexer> adviser

An adviser for a field, or an property or an indexer.

string template

The name of the template method. This method must have a single run-time parameter named value, and be annotated with the TemplateAttribute custom attribute.

ContractDirection direction

Direction of the data flow to which the contract should apply. See ContractDirection for details.

object args

An object (typically of anonymous type) whose properties map to parameters or type parameters of the template.

object tags

An optional opaque object of anonymous type passed to templates and exposed under the Tags property of the meta API.

Returns
Type Description
IAddContractAdviceResult<IFieldOrPropertyOrIndexer>