AttributeProviderAttribute 類別

定義

啟用屬性重新導向。Enables attribute redirection. 這個類別無法被繼承。This class cannot be inherited.

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

範例

下列程式碼範例將示範AttributeProviderAttribute如何使用來DataSource標示具有特定類型的IListSource屬性。The following code example demonstrates using AttributeProviderAttribute to mark a DataSource property with a specific type of IListSource. 如需完整的程式代碼清單, 請參閱如何:在 Windows Forms 控制項中套用屬性。For a full code listing, see How to: Apply Attributes in Windows Forms Controls.

[Category("Data")]
[Description("Indicates the source of data for the control.")]
[RefreshProperties(RefreshProperties.Repaint)]
[AttributeProvider(typeof(IListSource))]
public object DataSource
{
    get
    {
        return this.dataGridView1.DataSource;
    }

    set
    {
        this.dataGridView1.DataSource = value;
    }
}
<Category("Data"), _
Description("Indicates the source of data for the control."), _
RefreshProperties(RefreshProperties.Repaint), _
AttributeProvider(GetType(IListSource))> _
Public Property DataSource() As Object
    Get
        Return Me.dataGridView1.DataSource
    End Get

    Set(ByVal value As Object)
        Me.dataGridView1.DataSource = value
    End Set
End Property

備註

在 .NET Framework 物件模型中, 有一些案例是刻意將屬性類型設為不清楚。There are a few cases in the .NET Framework object model where a property is purposely typed to be vague. 例如, 屬性的DataGridView.DataSource類型為。 objectFor example, the DataGridView.DataSource property is typed as object. 這是因為這個屬性可以接受數種輸入類型。The reason for this is that this property can accept several types of input. 可惜的是, 這不會提供一般位置來加入中繼資料來描述屬性的特性。Unfortunately, this provides no common place to add metadata to describe the characteristics of the property. 整個DataSource .NET Framework 中的每個屬性都必須要有相同的中繼資料可用於類型轉換器、UI 類型編輯器, 以及其他需要中繼資料的服務。Each DataSource property throughout the .NET Framework needs to have identical metadata for type converters, UI type editors, and other services that require metadata. AttributeProviderAttribute種情況的解決辦法。The AttributeProviderAttribute remedies this situation.

一旦將這個屬性放在屬性上, 取得屬性描述元集合屬性的規則就MemberDescriptor.Attributes會有所不同。Once this attribute is placed on a property, the rules for obtaining attributes for the property descriptor's MemberDescriptor.Attributes collection differ. 一般而言, 屬性描述項會收集本機屬性, 然後將這些屬性與屬性類型的屬性合併。Typically, the property descriptor gathers local attributes, and then merges these with attributes from the property type. 在此情況下, 會從傳回AttributeProviderAttribute的型別 (而不是從實際的屬性型別) 取得屬性。In this case, the attributes are taken from the type returned from the AttributeProviderAttribute, not from the actual property type. 這個屬性會在上DataGridView.DataSource用來將DataGridView.DataSource IListSource物件的特定類型指向, 並將適當的中繼資料放IListSource在上, 以啟用資料系結。This attribute is used on DataGridView.DataSource to point the DataGridView.DataSource object's specific type to IListSource, and the appropriate metadata is placed on IListSource to enable data binding. 在這種情況下, 外部方可以輕鬆地將中繼資料新增至所有資料來源。In so doing, external parties can easily add metadata to all data sources.

從中宣告的類型取得的AttributeProviderAttribute屬性, 在屬性類型的屬性與屬性上的屬性之間具有優先權。Attributes obtained from a type declared in the AttributeProviderAttribute have a priority in between the attributes of the property's type and the attributes on the property. 下列清單 (依優先順序排列) 會顯示一組完整的可用合併屬性:The following list, in priority order, shows the full set of available merged attributes:

  • Property 屬性Property Attributes

  • 屬性提供者屬性Attribute Provider Attributes

  • 屬性型別屬性Property Type Attributes

建構函式

AttributeProviderAttribute(String)

使用指定的型別名稱,初始化 AttributeProviderAttribute 類別的新執行個體。Initializes a new instance of the AttributeProviderAttribute class with the given type name.

AttributeProviderAttribute(String, String)

使用指定的型別名稱和屬性名稱,初始化 AttributeProviderAttribute 類別的新執行個體。Initializes a new instance of the AttributeProviderAttribute class with the given type name and property name.

AttributeProviderAttribute(Type)

使用指定的型別,初始化 AttributeProviderAttribute 類別的新執行個體。Initializes a new instance of the AttributeProviderAttribute class with the given type.

屬性

PropertyName

取得將擷取屬性 (Attribute) 的屬性 (Property) 名稱。Gets the name of the property for which attributes will be retrieved.

TypeId

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

(繼承來源 Attribute)
TypeName

取得傳入建構函式的組件限定型別名稱。Gets the assembly qualified type name passed into the constructor.

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

(繼承來源 Attribute)
GetHashCode()

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

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

適用於

另請參閱