属性(Attribute)の値取得
ClassもしくはPropertyに属性を指定し、その値を取得する。
クラス用属性クラスの生成
[AttributeUsage(AttributeTargets.Class)] public class ClassAttrAttribute : Attribute { private string _value; public ClassAttrAttribute(string value) { this._value = value; } public string Value { get { return this._value; } } }
- Attributeクラスに属性としてAttributeUsageを設定する。
- AttributeTargetsによって対象を設定することが可能だ
プロパティ用属性クラスの生成
[AttributeUsage(AttributeTargets.Property)] public class PropertyAttrAttribute : Attribute { private string _value; public PropertyAttrAttribute(string value) { this._value = value; } public string Value { get { return this._value; } } }
これで新しいクラスに以下の用に属性を設定することが可能になる。
TestAttrClass
[ClassAttr("クラス")] public class TestAttrClass { [PropertyAttr("名前")] public string Name; [PropertyAttr("住所")] public string Addr; }
属性情報の取得
属性の取得はICustomAttributeProviderのGetCustomAttributesより取得する。
ICustomAttributeProviderはType型やMemberInfo型、ParameterInfo型が継承しているため、
各型から変換することが可能だ
クラス属性の場合、Type型からICustomAttributeProviderを取り出す様にする。
public string getClassAttrValue() { ICustomAttributeProvider provider = (ICustomAttributeProvider)typeof(TestAttrClass); var attributes = provider.GetCustomAttributes(typeof(ClassAttrAttribute),false) as ClassAttrAttribute[]; return attributes != null && attributes.Any() ? attributes[0].Value : null; }
プロパティ属性の場合、MemberInfo型からICustomAttributeProviderを取り出す様にする。
public List<string> getMemberAttrValues() { List<string> list = new List<string>(); PropertyAttrAttribute attr; foreach (MemberInfo p in typeof(TestAttrClass).GetMembers()) { ICustomAttributeProvider provider = (ICustomAttributeProvider)p; var attributes = provider.GetCustomAttributes(typeof(PropertyAttrAttribute),false) as PropertyAttrAttribute[]; if (attr != null) list.Add(attr.Value); } }
このままだど使い勝手が悪いため、各属性クラスに属性クラスを返すメソッドを追加する。
ClassAttrAttribute
public static ClassAttrAttribute get(Type t) { if (t == null) return null; ICustomAttributeProvider provider = (ICustomAttributeProvider)t; var attributes = provider.GetCustomAttributes(typeof(ClassAttrAttribute),false) as ClassAttrAttribute[]; return attributes != null && attributes.Any() ? attributes[0] : null; }
PropertyAttrAttribute
public static PropertyAttrAttribute get(MemberInfo p) { if (p == null) return null; ICustomAttributeProvider provider = (ICustomAttributeProvider)p; var attributes = provider.GetCustomAttributes(typeof(PropertyAttrAttribute), false) as PropertyAttrAttribute[]; return attributes != null && attributes.Any() ? attributes[0] : null; }
使用例
[ClassAttr("クラス")] public class TestAttrClass { [PropertyAttr("名前")] public string Name; [PropertyAttr("住所")] public string Addr; //クラスに設定された属性値を取得する public string getClassAttrValue() { ClassAttrAttribute attr = ClassAttrAttribute.get(this.GetTyoe()); return attr != null ? attr.TableName : null; } //プロパティに設定された属性値リストを取得する。 public List<string> getMemberAttrValues() { List<string> list = new List<string>(); PropertyAttrAttribute attr; foreach (var p in this.GetType().GetMembers()) { attr = PropertyAttrAttribute.get(p); if (attr != null) list.Add(attr.ColumnName); } } }