DesignOnlyAttribute 類別

定義

指定屬性是否只能在設計階段進行設定。Specifies whether a property can only be set at design time.

public ref class DesignOnlyAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.All)]
public sealed class DesignOnlyAttribute : Attribute
type DesignOnlyAttribute = class
    inherit Attribute
Public NotInheritable Class DesignOnlyAttribute
Inherits Attribute
繼承
DesignOnlyAttribute
屬性

範例

下列範例會建立GetLanguage屬性。The following example creates a GetLanguage property. 屬性會以DesignOnlyAttribute標示。The property is marked with a DesignOnlyAttribute.

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

下一個範例會取得的值DesignOnlyAttributeThe next example gets the value of the DesignOnlyAttribute. 首先, 程式碼會PropertyDescriptorCollection取得具有物件之所有屬性的。First the code gets a PropertyDescriptorCollection with all the properties for the object. 接著, 它會將PropertyDescriptorCollection索引加入以GetLanguage尋找屬性。Next it indexes into the PropertyDescriptorCollection to find the GetLanguage property. 然後, 它會傳回這個屬性的屬性, 並將其儲存在 attributes 變數中。Then it returns the attributes for this property and saves them in the attributes variable.

然後, 此範例會列印屬性是否為 [僅限DesignOnlyAttribute設計] AttributeCollection, 方法是從抓取, 然後將它寫入主控台畫面。The example then prints whether the property is design only by retrieving DesignOnlyAttribute from the AttributeCollection, and writing it to the console screen.

// 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)["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.
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()))

備註

以設定為trueDesignOnlyAttribute標記成員只能在設計階段設定。Members marked with the DesignOnlyAttribute set to true can be set only at design time. 一般而言, 這些屬性只會在設計階段存在, 而且不會在執行時間對應至物件上的 real 屬性。Typically, these properties exist only at design time and do not correspond to a real property on the object at run time.

沒有屬性的成員, 或以DesignOnlyAttribute false設定的標示, 可以在執行時間設定。Members that either have no attribute or are marked with the DesignOnlyAttribute set to false can be set during run time. 預設為 falseThe default is false.

設定為DesignOnlyAttribute true的屬性, 其值會序列化為InitializeComponent .resx 檔案, 而不是方法。A property with the DesignOnlyAttribute set to true has its value serialized to the .resx file instead of the InitializeComponent method.

如需詳細資訊,請參閱屬性For more information, see Attributes.

建構函式

DesignOnlyAttribute(Boolean)

初始化 DesignOnlyAttribute 類別的新執行個體。Initializes a new instance of the DesignOnlyAttribute class.

欄位

Default

指定 DesignOnlyAttribute 的預設值,就是 NoSpecifies the default value for the DesignOnlyAttribute, which is No. 這個 static 欄位是唯讀的。This static field is read-only.

No

指定屬性可在設計階段或 Run Time 進行設定。Specifies that a property can be set at design time or at run time. 這個 static 欄位是唯讀的。This static field is read-only.

Yes

指定屬性只能在設計階段進行設定。Specifies that a property can be set only at design time. 這個 static 欄位是唯讀的。This static field is read-only.

屬性

IsDesignOnly

取得值,表示屬性是否只能在設計階段進行設定。Gets a value indicating whether a property can be set only at design time.

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,表示指定的物件值是否等於目前的 DesignOnlyAttributeReturns whether the value of the given object is equal to the current DesignOnlyAttribute.

GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IsDefaultAttribute()

判斷這個屬性是否為預設值。Determines if this attribute is the default.

Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。Retrieves the type information for an object, which can be used to get the type information for an interface.

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。Provides access to properties and methods exposed by an object.

(繼承來源 Attribute)

適用於

另請參閱