关于元数据存储区MetadateStore及AttributeTableBuilder

首先介绍几个类:

1.       TypeDescriptor

.NET Framework 提供了两种访问某类型的元数据的方式:通过 System.Reflection 命名空间中提供的反射 API,以及通过 TypeDescriptor 类。反射是可用于所有类型的通用机制,因为它是基于根 Object 类的 GetType 方法建立的。反射为某个类型返回的信息不可扩展,因为编译了目标类型后就不能对其进行修改。

相反,TypeDescriptor 是组件的可扩展检查机制:即实现 IComponent 接口的那些类。与反射不同的是,它并不检查方法。通过目标组件的 Site 中提供的几种服务,可以动态扩展 TypeDescriptorTypeDescriptor是一个比System.Reflection 中的类功能更强的反射实现类。

效率也要高很多。 
 

2.MetadataStore

元数据存储区是设计时元数据的存储位置. 使用MetadataStore类可以将自定义设计时属性附加到类型。在用 AttributeTableBuilder 创建的 AttributeTable 中指定自定义属性。

通过使用 AddAttributeTable 方法将该属性表添加到元数据存储区中。添加后,调用 TypeDescriptor 时便会显示这些属性。
 

3. AttributeTable AttributeTableBuilder

AttributeTable包含了定义设计时外表(apperance)和行为(behavior)的元数据(metadata)属性(attribute)。AttributeTable这个sealed的类并没有定义一个公共的构造函数。而且实际上是一个只读字典,但其键和值分别进行计算,其内容对外是只读的。需要使用AttributeTableBuilderBuilder模式)。AddCustomAttributes()可用来添加新的属性。AddTable()则是将现有的AttributeTable内容全部加入到正在创建的AttributeTable之中。

当所有定义设计时外表和行为的元数据属性加入到AttributeTableBuilder之后,我们通过调用CreateTable()返回一个AttributeTable的实例。

 

下面是例子:

using System;

using System.Activities.Presentation.Metadata;

using System.ComponentModel;

 

namespace CaryMetadataStore

{

    class MetadataStoreDemo

    {

        static void Main(string[] args)

        {

            // 输出string上默认的属性

            AttributeCollection attributeCollection = TypeDescriptor.GetAttributes(typeof(string));

            Console.WriteLine("--------- 默认属性");

            OutputAttributes(attributeCollection);

 

            // 使用AttributeTableBuilderstring添加一个新的属性

            AttributeTableBuilder builder = new AttributeTableBuilder();

            builder.AddCustomAttributes(typeof(string), new DesignerCategoryAttribute("Custom category"));

            MetadataStore.AddAttributeTable(builder.CreateTable());

          

            Console.WriteLine("--------- 包含自定义属性");

            attributeCollection = TypeDescriptor.GetAttributes(typeof(string));

            OutputAttributes(attributeCollection);

            Console.WriteLine("--------- 注册回调函数");

 

           //使用AttributeCallback延时注册元数据(请求时)?

            builder = new AttributeTableBuilder();

            builder.AddCallback(typeof(string),

                new AttributeCallback(acb =>

                    {

                        Console.WriteLine("*** In AttributeCallback, 增加一个新的属性");

                        acb.AddCustomAttributes(new DesignTimeVisibleAttribute(false));

                    }

                )

            );

            MetadataStore.AddAttributeTable(builder.CreateTable());

 

            Console.WriteLine("--------- 包含通过回调函数增加的自定义属性");

            attributeCollection = TypeDescriptor.GetAttributes(typeof(string));

            OutputAttributes(attributeCollection);

            Console.WriteLine("Press Enter to Exit");

            Console.ReadLine();

        }

 

        private static void OutputAttributes(AttributeCollection attributeCollection)

        {

            foreach (Attribute attribute in attributeCollection)

            {

                Console.WriteLine("Attribute: {0}", attribute.ToString());

            }

        }

    }

} 

结果如下:

--------- 默认属性

Attribute: System.Reflection.DefaultMemberAttribute

Attribute: System.Runtime.InteropServices.ComVisibleAttribute

Attribute: System.SerializableAttribute

Attribute: System.CLSCompliantAttribute

Attribute: System.Runtime.CompilerServices.TypeDependencyAttribute

--------- 包含自定义属性

Attribute: System.ComponentModel.DesignerCategoryAttribute

Attribute: System.Reflection.DefaultMemberAttribute

Attribute: System.Runtime.InteropServices.ComVisibleAttribute

Attribute: System.SerializableAttribute

Attribute: System.CLSCompliantAttribute

Attribute: System.Runtime.CompilerServices.TypeDependencyAttribute

Attribute: System.Runtime.InteropServices.GuidAttribute

--------- 注册回调函数

--------- 包含通过回调函数增加的自定义属性

*** In AttributeCallback, 增加一个新的属性

Attribute: System.ComponentModel.DesignTimeVisibleAttribute

Attribute: System.ComponentModel.DesignerCategoryAttribute

Attribute: System.Reflection.DefaultMemberAttribute

Attribute: System.Runtime.InteropServices.ComVisibleAttribute

Attribute: System.SerializableAttribute

Attribute: System.CLSCompliantAttribute

Attribute: System.Runtime.CompilerServices.TypeDependencyAttribute

Attribute: System.Runtime.InteropServices.GuidAttribute

Press Enter to Exit

 

 

原文地址:https://www.cnblogs.com/carysun/p/MetadateStore.html