1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
<?xml version="1.0" encoding="utf-8"?>
<Type Name="ContractArgumentValidatorAttribute" FullName="System.Diagnostics.Contracts.ContractArgumentValidatorAttribute">
<TypeSignature Language="C#" Value="public sealed class ContractArgumentValidatorAttribute : Attribute" />
<TypeSignature Language="ILAsm" Value=".class public auto ansi sealed beforefieldinit ContractArgumentValidatorAttribute extends System.Attribute" />
<AssemblyInfo>
<AssemblyName>mscorlib</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Base>
<BaseTypeName>System.Attribute</BaseTypeName>
</Base>
<Interfaces />
<Attributes>
<Attribute>
<AttributeName>System.AttributeUsage(System.AttributeTargets.Method)</AttributeName>
</Attribute>
<Attribute>
<AttributeName>System.Diagnostics.Conditional("CONTRACTS_FULL")</AttributeName>
</Attribute>
</Attributes>
<Docs>
<remarks>
<attribution license="cc4" from="Microsoft" modified="false" />
<para>If your code uses explicit if-then-throw code to validate parameters, you may be employing helper methods that perform checks and throw particular exceptions on failure, as shown in the following example.</para>
<code>static class ValidationHelper
{
public static void NotNull(object argument, string parameterName)
{
if (argument == null) throw new ArgumentNullException(parameterName, ...);
}
}
...
public void MyMethod(string value)
{
ValidationHelper .NotNull(value , "value");
...
}
</code>
<para>In this example, MyMethod has an elective precondition specifying that the parameter value should not be null. To enable the contract tools to recognize that the call to ValidationHelper.NotNull represents a contract, you can mark the called method with the ContractArgumentValidator attribute. The EndContractBlock() call should be used to enable the tools to extract the proper specifications for document generation and static checking, as follows.</para>
<code>static class ValidationHelper {
[ContractArgumentValidator]
public static void NotNull(object argument, string parameterName) {
if (argument == null) throw new ArgumentNullException(parameterName, ...);
Contract.EndContractBlock();
...
}
}
</code>
<para>In addition to if-then-throw statements, the contract section of contract validator methods may contain calls to other contract validator methods. However, no other contracts (such as <see cref="M:System.Diagnostics.Contracts.Contract.Requires(System.Boolean)" />, or <see cref="M:System.Diagnostics.Contracts.Contract.Ensures(System.Boolean)" />) are allowed. Code that follows the EndContractBlock() call is ignored by all contract tools.The following example shows a range argument validator written in terms of an existing NotNull validator method.</para>
<code>static class ValidationHelper
{
[ContractArgumentValidator]
public static void NotNull(object argument, string parameterName)
{ ... }
[ContractArgumentValidator]
public static void InRange(object[] array , int index , string arrayName, string indexName) {
ValidationHelper .NotNull(array , arrayName);
if (index < 0) throw new ArgumentOutOfRangeException(indexName, ...);
if (index >= array.Length) throw new ArgumentOutOfRangeException(indexName, ...);
Contract.EndContractBlock();
...
}
...
public void MyMethod(int[] data, int position )
{
ValidationHelper .InRange(data, position , "data", " position ");
...
}
</code>
<para>From a specification point of view, the method MyMethod has the following three contracts:</para>
<code>Contract.Requires<ArgumentNullException>(data != null);
Contract.Requires<ArgumentOutOfRangeException>(position >= 0);
Contract.Requires<ArgumentOutOfRangeException>(position < data.Length);
</code>
<para>In standard methods, calls to contract validator methods can be freely mixed with other contracts such as <see cref="M:System.Diagnostics.Contracts.Contract.Ensures(System.Boolean)" /> or <see cref="M:System.Diagnostics.Contracts.Contract.Requires(System.Boolean)" />.</para>
</remarks>
<summary>
<attribution license="cc4" from="Microsoft" modified="false" />
<para>Enables the factoring of legacy if-then-throw code into separate methods for reuse, and provides full control over thrown exceptions and arguments.</para>
</summary>
</Docs>
<Members>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public ContractArgumentValidatorAttribute ();" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor() cil managed" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters />
<Docs>
<remarks>To be added.</remarks>
<summary>
<attribution license="cc4" from="Microsoft" modified="false" />
<para>Initializes a new instance of the <see cref="T:System.Diagnostics.Contracts.ContractArgumentValidatorAttribute" /> class.</para>
</summary>
</Docs>
</Member>
</Members>
</Type>
|