首先介绍几个类:
1. TypeDescriptor
.NET Framework 提供了两种访问某类型的元数据的方式:通过 System.Reflection 命名空间中提供的反射 API,以及通过 TypeDescriptor 类。反射是可用于所有类型的通用机制,因为它是基于根 Object 类的 GetType 方法建立的。反射为某个类型返回的信息不可扩展,因为编译了目标类型后就不能对其进行修改。
相反,TypeDescriptor 是组件的可扩展检查机制:即实现 IComponent 接口的那些类。与反射不同的是,它并不检查方法。通过目标组件的 Site 中提供的几种服务,可以动态扩展 TypeDescriptor。TypeDescriptor是一个比System.Reflection 中的类功能更强的反射实现类。
效率也要高很多。
2.MetadataStore
元数据存储区是设计时元数据的存储位置. 使用MetadataStore类可以将自定义设计时属性附加到类型。在用 AttributeTableBuilder 创建的 AttributeTable 中指定自定义属性。
通过使用 AddAttributeTable 方法将该属性表添加到元数据存储区中。添加后,调用 TypeDescriptor 时便会显示这些属性。
3. AttributeTable 与AttributeTableBuilder
AttributeTable包含了定义设计时外表(apperance)和行为(behavior)的元数据(metadata)属性(attribute)。AttributeTable这个sealed的类并没有定义一个公共的构造函数。而且实际上是一个只读字典,但其键和值分别进行计算,其内容对外是只读的。需要使用AttributeTableBuilder(Builder模式)。AddCustomAttributes()可用来添加新的属性。AddTable()则是将现有的AttributeTable内容全部加入到正在创建的AttributeTable之中。
当所有定义设计时外表和行为的元数据属性加入到AttributeTableBuilder之后,我们通过调用CreateTable()返回一个AttributeTable的实例。
下面是例子:
using System;
using System.Activities.Presentation.Metadata;
using System.ComponentModel;
namespace CaryMetadataStore
{
class MetadataStoreDemo
{
static void
{
// 输出string上默认的属性
AttributeCollection attributeCollection = TypeDescriptor.GetAttributes(typeof(string));
Console.WriteLine("--------- 默认属性");
OutputAttributes(attributeCollection);
// 使用AttributeTableBuilder给string添加一个新的属性
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 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 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——大语言模型本地部署的极速利器