2011-01-14 5 views
0

私は、従業員クラスがあります:私は私のようにCustomAttributeを使用していますデフォルトの属性リストは、C#.NETで私のクラス定義に来るべき

public class CustomAttributes : Attribute 
{ 

     public int Sequence {set;get;} 
     public int Length { set; get; } 

} 

としてカスタム属性CustomeAttributeを作成している

public class Employee 
{ 

     public string Name { get; set; } 
     public Int32 Salary { get; set; } 
     public DateTime DateOfBirth { get; set; } 
} 

とをEmployeeの属性として

public class Employee 
{ 

    public string Name { get; set; } 
    public Int32 Salary { get; set; } 
    public DateTime DateOfBirth { get; set; } 
} 

public class Employee 
{ 

    [CustomAttributes(Sequence=1,Length=10)] 
    public string Name { get; set; } 
    [CustomAttributes(Sequence = 2, Length= 6)] 
    public Int32 Salary { get; set; } 
    [CustomAttributes(Sequence =3, Length = 8)] 
    public DateTime DateOfBirth { get; set; } 
} 

attriを検証したいそれぞれのプロパティー定義に対してbuteコレクションが存在する必要があります。 私は

public class Employee 
{ 
    [CustomAttributes(Sequence=1,Length=10)] 
    public string Name { get; set; } 
    [CustomAttributes(Sequence = 2, Length= 6)] 
    public Int32 Salary { get; set; } 
    [CustomAttributes(Sequence =3, Length = 8)] 
    public DateTime DateOfBirth { get; set; } 

    public int Age {get;set;} 
} 

として'Employee`に新しいプロパティAgeを追加した場合、私は、属性が欠落しているように、時間コンパイルエラー取得する必要があります。これは、そのクラスの各プロパティと同じアセンブリからのクラスへの属性値の書き込みを確実にします。

属性値のないプロパティのコンパイル時エラーが発生するはずです。

答えて

0

このためにコンパイル時エラーが発生することはありません。しかし、あなたのユニットテストや#debugビルドで、何かを行うことができます - 単純にプロパティを繰り返しチェックします。Attribute.IsDefined(property, typeof(CustomAttributes))

+0

ありがとうございました。つまり、設計時に確認できません。 – pramodchoudhari

関連する問題