Open sandboxFocus
  • TOC

Metalama Documentation

Getting started

Conceptual documentation

Commented examples

API documentation

Aspect API

Metalama.​Framework.​Advising

Metalama.​Framework.​Aspects

Metalama.​Framework.​Code

Metalama.​Framework.​Code.​Collections

Metalama.​Framework.​Code.​Comparers

Metalama.​Framework.​Code.​Declaration­Builders

Metalama.​Framework.​Code.​Invokers

Metalama.​Framework.​Code.​Syntax­Builders

Metalama.​Framework.​Code.​Types

Metalama.​Framework.​Diagnostics

Metalama.​Framework.​Eligibility

Metalama.​Framework.​Fabrics

Metalama.​Framework.​Metrics

Metalama.​Framework.​Options

Metalama.​Framework.​Project

Metalama.​Framework.​Run­Time

Metalama.​Framework.​Serialization

Metalama.​Framework.​Services

Metalama.​Framework.​Utilities

Extensions API

Patterns API

Flashtrace API

Introspection API

Advanced API

Post­Sharp API migration

  • Article

Method AddContract

AddContract(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, ...).

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

The parameter to which the contract should be added.

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 tags

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

object args

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

Returns
Type Description
IAddContractAdviceResult<IParameter>

AddContract(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.

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

The field, property or indexer to which the contract should be added.

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 tags

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

object args

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

Returns
Type Description
IAddContractAdviceResult<IFieldOrPropertyOrIndexer>