Contract for verifying the implementation of an immutable type.

Namespace:  MbUnit.Framework.ContractVerifiers
Assembly:  MbUnit (in MbUnit.dll) Version: 3.3.0.0 (3.3.610.0)

Syntax

C#
public class ImmutabilityContract<TTarget> : AbstractContract
Visual Basic (Declaration)
Public Class ImmutabilityContract(Of TTarget) _
	Inherits AbstractContract

Type Parameters

TTarget
The target immutable type.

Remarks

Built-in verifications:

  • AreReadOnlyFields : All the public and non-public instance fields are marked as read only. The evaluation is made recursively on the field types too.
  • HasNoPublicPropertySetter : The type does not have any public property setter. The evaluation is made recursively on the property types too.

Examples

The following example shows a simple immutable class with all the instance fields marked as read only, and a test fixture which invokes the immutability contract to test the class.
CopyC#
public class SampleImmutable
{
    private readonly int number;
    private readonly string text;

    public SampleImmutable(int number, string text)
    {
        this.number = number;
        this.text = text;
    }
}

public class SampleImmutableTest
{
    [VerifyContract]
    public readonly IContract ImmutabilityTests = new ImmutabilityContract<SampleImmutable>();
}

Inheritance Hierarchy

System..::.Object
  MbUnit.Framework.ContractVerifiers..::.AbstractContract
    MbUnit.Framework.ContractVerifiers..::.ImmutabilityContract<(Of <(TTarget>)>)

See Also