2012-02-22 11 views
0

System.ComponentModel.DataAnnotationsアセンブリを使用して、私が取り組んでいるコンソールアプリケーションの引数(プロパティにマップされている)を検証します。私は "バディークラス"のメタデータパターンを使用します。それは過去に私のためにうまくいきました。Reflectionを使用したカスタム検証属性?

私が検証する必要があることの1つは、2つのタイプの引数のうちの1つが提供されていることです。つまり、引数foo、または引数barを指定できますが、両方を指定することはできません。

この目的のために、私はかなり簡単なように見えるカスタム検証属性を書き始めましたが、検証コンテキストのプロパティの外に到達する必要があり、オブジェクト内の兄弟プロパティ私は(CompareAttributeのように)検証しています。これは古典的な反省のケースだと思われますが、私はどのように進行するかについて私の頭を傷つけています。これはこれまで私が持っているものです:

/// <summary> 
/// This property, or the other specified, are required, but both cannot be set. 
/// </summary> 
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] 
public class XORAttribute : ValidationAttribute 
{ 
    /// <summary> 
    /// If validation should fail, return this error message. 
    /// </summary> 
    public string ErrorMessage { get; set; } 
    /// <summary> 
    /// The name of the other required property that is mutually exclusive of this one. 
    /// </summary> 
    public string OtherValueName { get; set; } 

    public XORAttribute(string otherValueName) 
    { 
     this.OtherValueName = otherValueName; 
    } 

    protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
    { 
     //Something needs to go here. 
    } 
} 

ここにいくつかの援助があります。

+0

本当に?テイカーはありませんか?私はこれが検証で経験のあるpplにとっては簡単な質問だと思っていたでしょう。 –

答えて

関連する問題