随笔 - 234, 文章 - 12, 评论 - 1671, 阅读 - 74万
  博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

关于元数据存储区MetadateStore及AttributeTableBuilder

Posted on   生鱼片  阅读(1856)  评论(2编辑  收藏  举报

首先介绍几个类:

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

 

 

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
点击右上角即可分享
微信分享提示