DefaultPropertyAttribute 類別

定義

指定元件的預設屬性。Specifies the default property for a component.

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

範例

下列範例會定義名為MyControl的控制項。The following example defines a control named MyControl. 類別會以DefaultPropertyAttribute指定MyProperty為預設屬性的來標示。The class is marked with a DefaultPropertyAttribute that specifies MyProperty as the default property.

[DefaultProperty("MyProperty")]
ref class MyControl: public Control
{
public:

   property int MyProperty 
   {
      int get()
      {
         // Insert code here.
         return 0;
      }

      void set( int value )
      {
         // Insert code here.
      }
   }
   // Insert any additional code.
};
[DefaultProperty("MyProperty")]
 public class MyControl : Control {
 
    public int MyProperty {
       get {
          // Insert code here.
          return 0;
       }
       set {
          // Insert code here.
       }
    }
 
    // Insert any additional code.
 
 }
<DefaultProperty("MyProperty")> _
Public Class MyControl
    Inherits Control

    Public Property MyProperty() As Integer
        Get
            ' Insert code here.
            Return 0
        End Get
        Set
            ' Insert code here.
        End Set 
    End Property
    ' Insert any additional code.
End Class

下一個範例會建立的實例MyControlThe next example creates an instance of MyControl. 然後, 它會取得類別的屬性、解壓縮DefaultPropertyAttribute, 並列印預設屬性的名稱。Then it gets the attributes for the class, extracts the DefaultPropertyAttribute, and prints the name of the default property.

int main()
{
   // Creates a new control.
   Form1::MyControl^ myNewControl = gcnew Form1::MyControl;

   // Gets the attributes for the collection.
   AttributeCollection^ attributes = TypeDescriptor::GetAttributes( myNewControl );

   /* Prints the name of the default property by retrieving the 
       * DefaultPropertyAttribute from the AttributeCollection. */
   DefaultPropertyAttribute^ myAttribute = dynamic_cast<DefaultPropertyAttribute^>(attributes[ DefaultPropertyAttribute::typeid ]);
   Console::WriteLine( "The default property is: {0}", myAttribute->Name );
   return 0;
}
public static int Main() {
    // Creates a new control.
    MyControl myNewControl = new MyControl();
 
    // Gets the attributes for the collection.
    AttributeCollection attributes = TypeDescriptor.GetAttributes(myNewControl);
 
    /* Prints the name of the default property by retrieving the 
     * DefaultPropertyAttribute from the AttributeCollection. */
    DefaultPropertyAttribute myAttribute = 
       (DefaultPropertyAttribute)attributes[typeof(DefaultPropertyAttribute)];
    Console.WriteLine("The default property is: " + myAttribute.Name);
  
    return 0;
 }
Public Shared Function Main() As Integer
    ' Creates a new control.
    Dim myNewControl As New MyControl()
    
    ' Gets the attributes for the collection.
    Dim attributes As AttributeCollection = TypeDescriptor.GetAttributes(myNewControl)
    
    ' Prints the name of the default property by retrieving the
    ' DefaultPropertyAttribute from the AttributeCollection. 
    Dim myAttribute As DefaultPropertyAttribute = _
        CType(attributes(GetType(DefaultPropertyAttribute)), DefaultPropertyAttribute)
    Console.WriteLine(("The default property is: " + myAttribute.Name))
    Return 0
End Function 'Main

備註

Name使用屬性來取得預設屬性的名稱。Use the Name property to get the name of the default property.

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

建構函式

DefaultPropertyAttribute(String)

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

欄位

Default

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

屬性

Name

取得與這個屬性 (Attribute) 繫結的元件的預設屬性 (Property) 名稱。Gets the name of the default property for the component this attribute is bound to.

TypeId

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

(繼承來源 Attribute)

方法

Equals(Object)

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

GetHashCode()

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

GetType()

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

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(繼承來源 Attribute)
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)

適用於

另請參閱