メモ: C#のカスタムAttributeの作り方
昨日初めて使ったのでメモしておきます。
カスタムAttributeを作る目的としては、次のものが多いみたいですね。
カスタムAttributeクラスの作り方
- System.Attributeを継承するクラスを作成する
- そのクラスにAttributeUsageAttributeを付けて、ターゲットを指定する
- クラス、フィールド、メソッド、プロパティなど複数指定もできる
- 指定可能な要素は、AttributeTargets 列挙体 (System)を参照
Attributeに引数がない時
マーカーアノテーション的に使うようなときはこちらです。クラスの中身はカラッポでOK。
[System.AttributeUsage(System.AttributeTargets.Property] public class FooAttribute : System.Attribute { }
Attributeに引数がある時
一層上に抽象クラスを定義してあげると便利なようです。
public abstract class AbstractAttribute : System.Attribute { public abstract void Process(ModelMetadata data); }
それを継承してカスタムAttributeを作成します。
[System.AttributeUsage(System.AttributeTargets.Property] public class FooAttribute : AbstractAttribute { private string bar; public FooAttribute(string bar) { this.bar = bar; } public override void Process(ModelMetadata data) { // たとえば data.AdditionalValues.Add("Foo", bar); } }
カスタムAttributeを付けたモノへの処理の書き方
ケースバイケースですが、ASP.NET MVC 4を使うなら、System.Web.Mvc.DataAnnotationsModelMetadataProviderクラスを継承したカスタムプロバイダで、CreateMetadataメソッドをオーバーライドして、次のように書けます。
引数がないカスタムAttributeのとき
protected override ModelMetadata CreateMetadata( IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName) { var data = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); var foo = attributes.SingleOrDefault(a => a is FooAttribute); if(foo != null){ // FooAttributeが付いていた場合にやりたいこと // data = ... } return data; }
引数があるカスタムAttributeのとき
protected override ModelMetadata CreateMetadata( IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName) { var data = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); attributes.OfType<FooAttribute>().ToList().ForEach(x => x.Process(data)); return data; }
参考
- Creating your own modelmetadataprovider to handle custom attributes
- チームの人に教えてもらったページm(_ _)m ありがとうございます。
- How to add custom Data Annotation attribute to your property in Asp.Net MVC
- Creating Custom Attributes (C# and Visual Basic)
- Attribute クラス
- 属性(C# によるプログラミング入門)
- 「属性の自作」という項目が参考になります。