ContractArgumentValidatorAttribute クラス

定義

再利用できるように異なるメソッドへのレガシ if-then-throw コードのファクタリングを有効にし、スローされる例外と引数のフル コントロールを提供します。

public ref class ContractArgumentValidatorAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false)]
[System.Diagnostics.Conditional("CONTRACTS_FULL")]
public sealed class ContractArgumentValidatorAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple=false)>]
[<System.Diagnostics.Conditional("CONTRACTS_FULL")>]
type ContractArgumentValidatorAttribute = class
    inherit Attribute
Public NotInheritable Class ContractArgumentValidatorAttribute
Inherits Attribute
継承
ContractArgumentValidatorAttribute
属性

注釈

コードで明示的な if-then-throw コードを使用してパラメーターを検証する場合は、次の例に示すように、チェックを実行し、エラー時に特定の例外をスローするヘルパー メソッドを使用している可能性があります。

using System;

static class ValidationHelper
{
   public static void NotNull(object argument, string parameterName)
   {
      if (argument == null) throw new ArgumentNullException(parameterName,
                                                            "The parameter cannot be null.");
   }
}

public class Example
{
   public void Execute(string value)
   {
      ValidationHelper.NotNull(value, "value");

      // Body of method goes here.
   }
}
Class ValidationHelper 
   Public Shared Sub NotNull(argument As Object, parameterName As String) 
      If argument Is Nothing Then 
         Throw New ArgumentNullException(parameterName, 
                                         "The parameter cannot be null.")
      End If
   End Sub
End Class

Module Example
   Public Sub Execute(value As String)
      ValidationHelper.NotNull(value, "value")
      
      ' Body of method goes here.
   End Sub
End Module

この例では、 Execute には、パラメーター値を にしないことを指定する選択前提条件があります null。 コントラクト ツールで、 の呼び出しがコントラクトを ValidationHelper.NotNull 表していることを認識できるようにするには、呼び出されたメソッドを 属性で ContractArgumentValidatorAttribute マークします。 Contract.EndContractBlockメソッド呼び出しを使用して、ツールでドキュメント生成と静的チェックの適切な仕様を次のように抽出できるようにする必要があります。

using System;
using System.Diagnostics.Contracts;

static class ValidationHelper
{
   [ContractArgumentValidator]
   public static void NotNull(object argument, string parameterName)
   {
      if (argument == null) throw new ArgumentNullException(parameterName,
                                                            "The parameter cannot be null.");
      Contract.EndContractBlock();
   }
}
Imports System.Diagnostics.Contracts

Class ValidationHelper 
   <ContractArgumentValidator> 
   Public Shared Sub NotNull(argument As Object, parameterName As String) 
      If argument Is Nothing Then 
         Throw New ArgumentNullException(parameterName, 
                                         "The parameter cannot be null.")
         Contract.EndContractBlock()
      End If
   End Sub
End Class

ステートメントに if-then-throw 加えて、コントラクト検証コントロール メソッドのコントラクト セクションには、他のコントラクト検証コントロール メソッドの呼び出しが含まれる場合があります。 ただし、他のコントラクト (、 Contract.EnsuresなどContract.Requires) は許可されません。 呼び出しに Contract.EndContractBlock 続くコードは、すべてのコントラクト ツールでは無視されます。

次の例は、既存 NotNull の検証メソッドの観点から記述された範囲引数検証コントロールを示しています。

using System;
using System.Diagnostics.Contracts;

static class ValidationHelper
{
   [ContractArgumentValidator]
   public static void NotNull(object argument, string parameterName)
   {
      if (argument == null) throw new ArgumentNullException(parameterName,
                                                            "The parameter cannot be null.");
      Contract.EndContractBlock();
   }

   [ContractArgumentValidator]
   public static void InRange(object[] array, int index, string arrayName, string indexName)
   {
      NotNull(array, arrayName);

      if (index < 0)
         throw new ArgumentOutOfRangeException(indexName,
                                               "The index cannot be negative.");
      if (index >= array.Length)
         throw new ArgumentOutOfRangeException(indexName,
                                               "The index is outside the bounds of the array.");
      Contract.EndContractBlock();
   }
}

public class Example
{
   public void Execute(object[] data, int position)
   {
      ValidationHelper.InRange(data, position, "data", "position");

      // Body of method goes here.
   }
}
Imports System.Diagnostics.Contracts

Class ValidationHelper 
   <ContractArgumentValidator> 
   Public Shared Sub NotNull(argument As Object, parameterName As String) 
      If argument Is Nothing Then 
         Throw New ArgumentNullException(parameterName, 
                                         "The parameter cannot be null.")
         Contract.EndContractBlock()
      End If
   End Sub

   <ContractArgumentValidator>
   Public Shared Sub InRange(array() As Object, index As Integer, 
                             arrayName As String, indexName As String)
      NotNull(array, arrayName)
      
      If index < 0 Then 
         Throw New ArgumentOutOfRangeException(indexName, 
                                               "The index cannot be negative.")
      End If                                         
      If index >= array.Length Then 
         Throw New ArgumentOutOfRangeException(indexName, 
                                               "The index is outside the bounds of the array.")
      End If                                                                                              
      Contract.EndContractBlock()
   End Sub
End Class

Module Example
   Public Sub Execute(data() As Object, position As Integer)
      ValidationHelper.InRange(data, position, "data", "position")
      
      ' Body of method goes here.
   End Sub
End Module

仕様の観点から、 Execute メソッドには次の 3 つのコントラクトがあります。

Contract.Requires<ArgumentNullException>(data != null);  
Contract.Requires<ArgumentOutOfRangeException>(position >= 0);  
Contract.Requires<ArgumentOutOfRangeException>(position < data.Length);  

標準メソッドでは、コントラクト検証コントロール メソッドの呼び出しを、 や Contract.RequiresなどのContract.Ensures他のコントラクトと自由に混在させることができます。

コンストラクター

ContractArgumentValidatorAttribute()

ContractArgumentValidatorAttribute クラスの新しいインスタンスを初期化します。

プロパティ

TypeId

派生クラスで実装されると、この Attribute の一意の識別子を取得します。

(継承元 Attribute)

メソッド

Equals(Object)

このインスタンスが、指定されたオブジェクトと等価であるかどうかを示す値を返します。

(継承元 Attribute)
GetHashCode()

このインスタンスのハッシュ コードを返します。

(継承元 Attribute)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
IsDefaultAttribute()

派生クラスでオーバーライドされるとき、このインスタンスの値が派生クラスの既定値であるかどうかを示します。

(継承元 Attribute)
Match(Object)

派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。

(継承元 Attribute)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

一連の名前を対応する一連のディスパッチ識別子に割り当てます。

(継承元 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

オブジェクトの型情報を取得します。この情報はインターフェイスの型情報の取得に使用できます。

(継承元 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。

(継承元 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されたプロパティおよびメソッドへのアクセスを提供します。

(継承元 Attribute)

適用対象