AttributeTable Class

A table of metadata attributes for defining design-time appearance and behavior.

Inheritance Hierarchy

System.Object
  Microsoft.Windows.Design.Metadata.AttributeTable

Namespace:  Microsoft.Windows.Design.Metadata
Assembly:  Microsoft.Windows.Design.Extensibility (in Microsoft.Windows.Design.Extensibility.dll)

Syntax

'Declaration
Public NotInheritable Class AttributeTable
public sealed class AttributeTable
public ref class AttributeTable sealed
[<Sealed>]
type AttributeTable =  class end
public final class AttributeTable

The AttributeTable type exposes the following members.

Properties

  Name Description
Public property AttributedTypes Gets an enumeration of all types which have attribute overrides of some kind, for example, on a property or on the type itself.

Top

Methods

  Name Description
Public method ContainsAttributes Returns a value indicating whether this table contains any metadata for the specified type.
Public method Equals Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetCustomAttributes(Assembly) Returns an enumeration of all attributes provided for the specified assembly.
Public method GetCustomAttributes(Type) Returns an enumeration of all attributes provided for the specified type.
Public method GetCustomAttributes(Type, String) Returns an enumeration of all attributes provided for the specified type and member name.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

Use the AttributeTable class to associate design-time metadata attributes with Windows Presentation Foundation (WPF) types.

To create an attribute table, call the CreateTable method of the AttributeTableBuilder class. For more information, see Providing Design-time Metadata.

An attribute table is essentially a read-only dictionary, but its keys and values are computed separately. It is efficient to query an attribute table if it contains attributes for a particular type. The actual set of attributes is demand created.

Examples

The following code example shows how to create and populate an attribute table. For more information, see Walkthrough: Creating a Design-time Adorner.

Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Reflection
Imports System.Text
Imports System.Windows.Media
Imports System.Windows.Controls
Imports System.Windows

Imports Microsoft.Windows.Design
Imports Microsoft.Windows.Design.Features
Imports Microsoft.Windows.Design.Metadata

' The ProvideMetadata assembly-level attribute indicates to designers
' that this assembly contains a class that provides an attribute table. 
<Assembly: ProvideMetadata(GetType(CustomControlLibrary.VisualStudio.Design.Metadata))> 

' Container for any general design-time metadata to initialize.
' Designers look for a type in the design-time assembly that 
' implements IProvideAttributeTable. If found, designers instantiate
' this class and access its AttributeTable property automatically.
Friend Class Metadata
    Implements IProvideAttributeTable

    ' Accessed by the designer to register any design-time metadata.
    Public ReadOnly Property AttributeTable() As AttributeTable _
        Implements IProvideAttributeTable.AttributeTable
        Get
            Dim builder As New AttributeTableBuilder()

            ' Apply the ReadOnlyAttribute to the Background property 
            ' of the Button class.
            builder.AddCustomAttributes(GetType(Button), "Background", New ReadOnlyAttribute(True))

            Dim attributes As AttributeTable = builder.CreateTable()

            Dim hasCustomAttributes As Boolean = attributes.ContainsAttributes(GetType(Button))

            Dim types As IEnumerable(Of Type) = attributes.AttributedTypes

            ' The following code shows how to retrieve custom attributes
            ' using the GetCustomAttributes method overloads.
            Dim attrs0 As IEnumerable = attributes.GetCustomAttributes(GetType(Button))

            Dim attrs1 As IEnumerable = attributes.GetCustomAttributes(GetType(Button), "Background")

            Return attributes
        End Get
    End Property
End Class
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows;

using Microsoft.Windows.Design.Features;
using Microsoft.Windows.Design.Metadata;

// The ProvideMetadata assembly-level attribute indicates to designers
// that this assembly contains a class that provides an attribute table. 
[assembly: ProvideMetadata(typeof(CustomControlLibrary.VisualStudio.Design.Metadata))]
namespace CustomControlLibrary.VisualStudio.Design
{
    // Container for any general design-time metadata to initialize.
    // Designers look for a type in the design-time assembly that 
    // implements IProvideAttributeTable. If found, designers instantiate 
    // this class and access its AttributeTable property automatically.
    internal class Metadata : IProvideAttributeTable
    {
        // Accessed by the designer to register any design-time metadata.
        public AttributeTable AttributeTable
        {
            get
            {
                AttributeTableBuilder builder = new AttributeTableBuilder();

                // Apply the ReadOnlyAttribute to the Background property 
                // of the Button class.
                builder.AddCustomAttributes(
                    typeof(Button),
                    "Background",
                    new ReadOnlyAttribute(true));

                AttributeTable attributes = builder.CreateTable();

                bool hasCustomAttributes = attributes.ContainsAttributes(typeof(Button));

                IEnumerable<Type> types = attributes.AttributedTypes;

                // The following code shows how to retrieve custom attributes
                // using the GetCustomAttributes method overloads.
                IEnumerable attrs0 = attributes.GetCustomAttributes(typeof(Button));

                IEnumerable attrs1 = attributes.GetCustomAttributes(
                    typeof(Button),
                    "Background");

                return attributes;
            }
        }
    }
}

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Microsoft.Windows.Design.Metadata Namespace

AttributeTableBuilder

Other Resources

Providing Design-time Metadata