方法 : コンポーネントに関する説明用メタデータを提供する

"属性" を使用して、コンポーネントに関する説明用のメタデータを提供できます。 属性は、コード要素に適用される特別なクラスです。 コンパイル時に属性はメタデータに出力され、このメタデータは System.Reflection 名前空間をとおして共通言語ランタイムおよびカスタム ツールやカスタム アプリケーションで利用できます。

属性をコンポーネントに追加するには、コンポーネントの前に属性への参照を追加し、関連するパラメーターやフラグを指定します。 このコンストラクター呼び出しは、Visual Basic では山かっこ <> 内に、C# では角かっこ [] 内に記述します。

規則として、属性クラスはすべて "Attribute" で終わります。たとえば、DescriptionAttributeObsoleteAttribute、および BrowsableAttribute の各クラスがあります。 ただし、Visual Basic や C# など、共通言語ランタイムを使用するいくつかの言語では、属性の名前を完全に記述しなくてもかまいません。 たとえば、ObsoleteAttribute を参照する場合は、コード内で単に Obsolete と記述できます。

コンポーネントに既存の属性を追加するには

  1. コンポーネントに必要な属性を決定します。

  2. コンポーネントに属性を追加します。 属性の完全限定名を使用するか、または適切な Imports (using) ステートメントを使用する必要があります。 DescriptionAttribute 属性を追加する方法の例を次に示します。

    Imports System.ComponentModel
    <Description("This is a description string")> Public Class TheClass
    End Class
    
    using System.ComponentModel;
    [Description("This is a description string")]
    public class TheClass
    {
    }
    

カスタム属性

Attribute を継承することにより、独自のカスタム ツールまたはカスタム アプリケーションで使用するための独自の属性を作成することもできます。 この基本クラスに対して、アプリケーションで必要な任意のカスタム プロパティやカスタム メソッドを追加できます。

カスタム属性を作成して適用するには

  1. Attribute を継承するクラスを作成します。

    Public Class WidgetAttribute
       Inherits System.Attribute
    End Class
    
    public class WidgetAttribute: System.Attribute
    {
    }
    
  2. 属性に必要なプロパティおよびメソッドを決定し、そのコードを記述します。 WidgetAttribute クラスのコンストラクターで設定される WidgetType プロパティを作成する方法を次の例に示します。 AttributeUsageAttribute の属性は、コード メンバーを対象とする属性ができるかを設定します。

    <AttributeUsage(System.AttributeTargets.Class)> Public Class _
       WidgetAttribute
       Inherits System.Attribute
       Private mWidgetType as WidgetTypeEnum
       ' Creates a readonly property for the WidgetAttribute class.
       Public ReadOnly Property WidgetType as WidgetTypeEnum
          Get
             Return mWidgetType
          End Get
       End Property
       ' Creates a constructor that accepts a parameter and assigns the 
       ' value of that parameter to the WidgetType property.
       Public Sub New(type as WidgetTypeEnum)
          MyBase.New()
          mWidgetType = type
       End Sub
    End Class
    
    [AttributeUsage(System.AttributeTargets.Class)]
    public class WidgetAttribute: System.Attribute
    {
       private WidgetTypeEnum widgetType;
    
       // Creates a readonly property for the WidgetAttribute class.
       public WidgetTypeEnum WidgetType
          {
             get {return widgetType;}
          }
    
       public WidgetAttribute(WidgetTypeEnum type): base()
          {
             widgetType = type;
          }
    }
    
  3. 必要なパラメーターを適用して、他の属性の場合と同様にこの属性を適用します。

    <WidgetAttribute(WidgetTypeEnum.VerticalWidget)> _
       Public Class WidgetFortyFive
    End Class
    
    [WidgetAttribute(WidgetTypeEnum.VerticalWidget)]
    public class WidgetFortyFive
    {
    }
    

参照

処理手順

方法 : コンポーネントのプロパティ、メソッド、およびイベントに対するメタデータを提供する

関連項目

Attribute

概念

カスタム属性へのアクセス

属性に格納されている情報の取得

その他の技術情報

コンポーネントのユーザー支援