我在我的项目中添加了允许用户将他们自己的自定义属性添加到对象的功能。我已经创建了我自己的自定义TypeDescriptor、PropertyDescriptor和TypeDescriptorProviders等等..等等..来做到这一点。
这是我的问题。现在我已经完成了所有工作,但是必须为每个可以具有自定义属性的对象对象类型创建一个单独的TypeDescriptionProvider 。这是我的TypeDescriptionProviders的样子
//type AClass Custom Provider
class AClassTypeProvider : TypeDescriptionProvider
{
private static TypeDescriptionProvider defaultTypeProvider = TypeDescriptor.GetProvider(typeof(AClass));
public AClassTypeProvider (): base(defaultTypeProvider)
{
}
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
{
ICustomTypeDescriptor defaultDescriptor = base.GetTypeDescriptor(objectType, instance);
//returns a custom type descriptor based on a UserPropertyHostType enum value, and the default descriptor
return new InfCustomTypeDescriptor(UserPropertyHostType.SiteRegion, defaultDescriptor);
}
}
//type BClass Custom Provider
class BClassTypeProvider : TypeDescriptionProvider
{
private static TypeDescriptionProvider defaultTypeProvider = TypeDescriptor.GetProvider(typeof(BClass));
public BClassTypeProvider (): base(defaultTypeProvider)
{
}
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
{
ICustomTypeDescriptor defaultDescriptor = base.GetTypeDescriptor(objectType, instance);
//returns a custom type descriptor based on a UserPropertyHostType enum value, and the default descriptor
return new InfCustomTypeDescriptor(UserPropertyHostType.Building, defaultDescriptor);
}
}
因此,我的每个自定义TypeDescriptionProviders通过将特定类型的默认TypeDescriptionProvider传递给base(TypeDescriptionProvider parent)基本构造函数来调用它。
GetTypeDescriptor()方法调用base.GetTypeDescriptor()来获取默认描述符,然后我的自定义类型描述符使用该描述符添加自定义属性。
有没有办法将这些组合成一个具有相同功能但不绑定到特定类型的通用自定义TypeDescriptionProvider ?我是否可以跳过在构造函数中提供父TypeDescriptionProvider但稍后在我明确知道正在查询什么类型的对象时在GetTypeDescriptor()方法中设置它?还是有其他方法可以获取类型的默认描述符,然后调用base.GetTypeDescriptor(Type t,object ins)方法?