AttributeProviderAttribute 類別

定義

啟用屬性重新導向。 此類別無法獲得繼承。

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

範例

下列程式碼範例示範如何使用 AttributeProviderAttribute 來標記 DataSource 具有特定類型的 IListSource 屬性。 如需完整的程式代碼清單,請參閱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物件模型中,有一些情況是屬性的型別是模糊的。 例如, DataGridView.DataSource 屬性的類型為 object 。 這是因為這個屬性可以接受數種類型的輸入。 可惜的是,這不會提供新增中繼資料來描述屬性特性的常見位置。 整個.NET Framework中的每個 DataSource 屬性都必須有類型轉換器、UI 類型編輯器和其他需要中繼資料的服務相同的中繼資料。 此情況可 AttributeProviderAttribute 解決此情況。

在屬性上放置這個屬性之後,取得屬性描述項 MemberDescriptor.Attributes 集合屬性的規則會有所不同。 屬性描述項通常會收集本機屬性,然後將這些屬性與屬性類型中的屬性合併。 在此情況下,屬性取自 從 AttributeProviderAttribute 傳回的類型,而不是從實際屬性類型取得。 這個屬性用於 DataGridView.DataSource 將物件的特定類型 IListSource 指向 DataGridView.DataSource ,並放置 IListSource 適當的中繼資料以啟用資料系結。 因此,外部合作物件可以輕鬆地將中繼資料新增至所有資料來源。

從 中 AttributeProviderAttribute 宣告之型別取得的屬性,在 屬性類型的屬性與 屬性的屬性之間具有優先權。 下列清單會依優先順序顯示一組完整的可用合併屬性:

  • Property 屬性

  • 屬性提供者屬性

  • 屬性類型屬性

建構函式

AttributeProviderAttribute(String)

使用指定的型別名稱,初始化 AttributeProviderAttribute 類別的新執行個體。

AttributeProviderAttribute(String, String)

使用指定的型別名稱和屬性名稱,初始化 AttributeProviderAttribute 類別的新執行個體。

AttributeProviderAttribute(Type)

使用指定的型別,初始化 AttributeProviderAttribute 類別的新執行個體。

屬性

PropertyName

取得將擷取屬性 (Attribute) 的屬性 (Property) 名稱。

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)
TypeName

取得傳入建構函式的組件限定型別名稱。

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

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

擷取物件的類型資訊,可以用來取得介面的類型資訊。

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

擷取物件提供的類型資訊介面數目 (0 或 1)。

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

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱