Open sandboxFocus

Method AddContract

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

Adds a contract to a parameter. Contracts validate or normalize parameter values at different points in the data flow, typically used for precondition checks (input parameters) or postcondition checks (output parameters and return values). Use the With<TNewDeclaration>(TNewDeclaration) method to add the contract to a different parameter 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 TemplateAttribute.

ContractDirection direction

Direction of the data flow to which the contract should apply. Use Input for preconditions, Output for postconditions, or Default to select automatically based on parameter type. 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 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
IAddContractAdviceResult<IParameter>

An IAddContractAdviceResult<T> exposing the added contract.

Remarks

Template Access: Within the template, use meta.Target.Expression for unified access to the target as an IExpression, meta.Target.Parameter for parameter-specific access, and meta.Target.ContractDirection to determine whether you're validating input or output.

Performance Note: When possible, provide all contracts to the same method from a single aspect. This approach yields better compile-time performance than using several separate aspects.

Ready-Made Contracts: Consider using the Metalama.Patterns.Contracts package, which provides ready-made contract attributes for common validation scenarios (nullability, strings, numeric ranges, enums, collections). See Metalama.Patterns.ContractsMetalama.Patterns.Contracts for details.

See Also

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

Adds a contract to a field, property or indexer. Contracts validate or normalize values, typically used to validate assigned values (setters) or returned values (getters). Fields with contracts are automatically transformed into properties. Use the With<TNewDeclaration>(TNewDeclaration) method to add the contract to a different field, property or indexer 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, property or indexer.

string template

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

ContractDirection direction

Direction of the data flow to which the contract should apply. Use Input to validate values being assigned (setter), Output to validate values being retrieved (getter), or Default to select automatically. 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 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
IAddContractAdviceResult<IFieldOrPropertyOrIndexer>

An IAddContractAdviceResult<T> exposing the added contract.

Remarks

Template Access: Within the template, use meta.Target.Expression for unified access to the target as an IExpression, meta.Target.FieldOrProperty for field/property-specific access, and meta.Target.ContractDirection to determine whether you're validating input or output.

Performance Note: When possible, provide all contracts to the same method from a single aspect. This approach yields better compile-time performance than using several separate aspects.

Ready-Made Contracts: Consider using the Metalama.Patterns.Contracts package, which provides ready-made contract attributes for common validation scenarios (nullability, strings, numeric ranges, enums, collections). See Metalama.Patterns.ContractsMetalama.Patterns.Contracts for details.

See Also