AttributeProviderAttribute 类

定义

启用特性重定向。Enables attribute redirection. 此类不能被继承。This class cannot be inherited.

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

示例

下面的代码示例演示如何使用 AttributeProviderAttribute 来标记具有特定类型 IListSourceDataSource 属性。The following code example demonstrates using AttributeProviderAttribute to mark a DataSource property with a specific type of IListSource. 有关完整的代码清单,请参阅如何:在 Windows 窗体控件中应用特性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. 整个 .NET Framework 中的每个 DataSource 属性都需要具有相同的类型转换器、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.DataSourceDataGridView.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) 属性 (Attribute)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

获取将为其检索特性的属性的名称。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()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

显式接口实现

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

将一组名称映射为对应的一组调度标识符。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)

适用于

另请参阅