次の方法で共有


DesignOnlyAttribute クラス

プロパティを設定できるのがデザイン時だけかどうかを指定します。

名前空間: System.ComponentModel
アセンブリ: System (system.dll 内)

構文

'宣言
<AttributeUsageAttribute(AttributeTargets.All)> _
Public NotInheritable Class DesignOnlyAttribute
    Inherits Attribute
'使用
Dim instance As DesignOnlyAttribute
[AttributeUsageAttribute(AttributeTargets.All)] 
public sealed class DesignOnlyAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::All)] 
public ref class DesignOnlyAttribute sealed : public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.All) */ 
public final class DesignOnlyAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.All) 
public final class DesignOnlyAttribute extends Attribute

解説

DesignOnlyAttributetrue を設定してマークされているメンバは、デザイン時にだけ設定できます。通常、これらのプロパティはデザイン時しか存在せず、実行時にオブジェクト上に存在するプロパティには対応しません。

属性を持たないメンバ、または DesignOnlyAttributefalse に設定してマークされているメンバは、実行時に設定できます。既定値は false です。

DesignOnlyAttributetrue に設定されているプロパティは、値が InitializeComponent メソッドではなく .resx ファイルにシリアル化されます。

詳細については、属性の概要属性を使用したメタデータの拡張 の各トピックを参照してください。

使用例

GetLanguage プロパティを作成する例を次に示します。このプロパティは、DesignOnlyAttribute でマークされます。

<DesignOnly(True)> _
Public Property GetLanguage() As CultureInfo
    Get
        ' Insert code here.
        Return myCultureInfo
    End Get
    Set
        ' Insert code here.
    End Set
End Property
[DesignOnly(true)]
 public CultureInfo GetLanguage {
    get {
       // Insert code here.
       return myCultureInfo;
    }
    set {
       // Insert code here.
    }
 }
public:
   [DesignOnly(true)]
   property CultureInfo^ GetLanguage 
   {
      CultureInfo^ get()
      {
         // Insert code here.
         return myCultureInfo;
      }
      void set( CultureInfo^ value )
      {
         // Insert code here.
      }
   }
/** @attribute DesignOnly(true)
 */
/** @property 
 */
public CultureInfo get_GetLanguage()
{
    // Insert code here.
    return myCultureInfo;
} //get_GetLanguage

/** @property 
 */
public void set_GetLanguage(CultureInfo value)
{
    // Insert code here.
} //set_GetLanguage

DesignOnlyAttribute の値を取得する例を次に示します。最初に、オブジェクトのすべてのプロパティを保持する PropertyDescriptorCollection を取得します。次に、インデックスを付けて PropertyDescriptorCollection から GetLanguage プロパティを検索します。そして、このプロパティの属性を返し、その属性を属性変数に保存します。

最後に、AttributeCollection から DesignOnlyAttribute を取得し、取得した情報をコンソール画面に書き込むことによって、プロパティを設定できるのがデザイン時だけかどうかを出力します。

' Gets the attributes for the property.
Dim attributes As AttributeCollection = _
    TypeDescriptor.GetProperties(Me)("GetLanguage").Attributes

' Prints whether the property is marked as DesignOnly 
' by retrieving the DesignOnlyAttribute from the AttributeCollection.
Dim myAttribute As DesignOnlyAttribute = _
    CType(attributes(GetType(DesignOnlyAttribute)), DesignOnlyAttribute)
Console.WriteLine(("This property is design only :" & _
    myAttribute.IsDesignOnly.ToString()))
// Gets the attributes for the property.
 AttributeCollection attributes = 
    TypeDescriptor.GetProperties(this)["GetLanguage"].Attributes;

 /* Prints whether the property is marked as DesignOnly 
  * by retrieving the DesignOnlyAttribute from the AttributeCollection. */
 DesignOnlyAttribute myAttribute = 
    (DesignOnlyAttribute)attributes[typeof(DesignOnlyAttribute)];
 Console.WriteLine("This property is design only :" +
    myAttribute.IsDesignOnly.ToString());
// Gets the attributes for the property.
AttributeCollection^ attributes = TypeDescriptor::GetProperties( this )[ "GetLanguage" ]->Attributes;

/* Prints whether the property is marked as DesignOnly 
by retrieving the DesignOnlyAttribute from the AttributeCollection. */
DesignOnlyAttribute^ myAttribute = dynamic_cast<DesignOnlyAttribute^>(attributes[ DesignOnlyAttribute::typeid ]);
Console::WriteLine( "This property is design only :{0}", myAttribute->IsDesignOnly );
// Gets the attributes for the property.
AttributeCollection attributes = TypeDescriptor.GetProperties(this).
    get_Item("GetLanguage").get_Attributes();

/* Prints whether the property is marked as DesignOnly 
 by retrieving the DesignOnlyAttribute from the AttributeCollection. */
DesignOnlyAttribute myAttribute = (DesignOnlyAttribute)(attributes.
    get_Item(DesignOnlyAttribute.class.ToType()));

Console.WriteLine("This property is design only :" 
    + System.Convert.ToString(myAttribute.get_IsDesignOnly()));

継承階層

System.Object
   System.Attribute
    System.ComponentModel.DesignOnlyAttribute

スレッド セーフ

この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。

プラットフォーム

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 2.0、1.1、1.0

参照

関連項目

DesignOnlyAttribute メンバ
System.ComponentModel 名前空間
Attribute
PropertyDescriptor