特定のクラスの属性を取得し、それらを反復処理できるリフレクション ライブラリがあります。
リフレクションを使用して属性値とプロパティを反復処理する方法を理解したら、それらを検証に使用する方法を概念的に理解するのはそれほど難しいことではありません。
リフレクションを使用して、オブジェクトのメソッドとプロパティを反復処理し、それらのメソッド/プロパティを呼び出すこともできます。Microsoft には、これに関する非常に優れたドキュメントがいくつかあります。これを調べたい場合は、ゴーグルするだけでかまいません。
これがサンプルプログラムです。属性を利用する
class Program
{
static void Main(string[] args)
{
var something = new ClassWithAttributes();
var attributes = typeof(ClassWithAttributes).GetCustomAttributesData();
var attribute = (SomeAttribute) Attribute.GetCustomAttribute(typeof(ClassWithAttributes), typeof (SomeAttribute));
Console.WriteLine(attribute.Name);
Console.ReadKey(false);
}
}
[Some("larry")]
class ClassWithAttributes
{
}
public class SomeAttribute : System.Attribute
{
public string Name { get; set; }
public SomeAttribute(string name)
{
this.Name = name;
}
}
これが、そのサンプルを作成するために使用したドキュメントです
http://msdn.microsoft.com/en-us/library/sw480ze8.aspx
http://msdn.microsoft.com/en-us/library/71s1zwct%28v=vs.110%29.aspx