1

我在我的项目中添加了允许用户将他们自己的自定义属性添加到对象的功能。我已经创建了我自己的自定义TypeDescriptorPropertyDescriptorTypeDescriptorProviders等等..等等..来做到这一点。

这是我的问题。现在我已经完成了所有工作,但是必须为每个可以具有自定义属性的对象对象类型创建一个单独的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)方法?

4

2 回答 2

2

这个通用类应该做你想做的事:

class CustomTypeProvider<T> : TypeDescriptionProvider
{
    private static TypeDescriptionProvider defaultTypeProvider = TypeDescriptor.GetProvider(typeof(T));

    public CustomTypeProvider(): 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);
    }
}
于 2009-10-06T00:01:42.297 回答
1

我过去使用泛型类型处理过这个问题:

public class MyTypeDescriptionProvider<T> : TypeDescriptionProvider
{
  public MyTypeDescriptionProvider()
    : base(TypeDescriptor.GetProvider(typeof(T)))
  {
  }
}

我很确定您可以通过将类型作为构造函数参数传递来在非泛型类型中类似地处理它:

public class MyTypeDescriptionProvider : TypeDescriptionProvider
{
  public MyTypeDescriptionProvider(Type t)
    : base(TypeDescriptor.GetProvider(t))
  {
  }
}

如果您不需要在提供程序中使用该类型,这可能更可取——尽管尚未对其进行测试。

然后在使用这个提供者时,类按照以下方式注册:

TypeDescriptor.AddProvider(new MyTypeDescriptionProvider<ClassA>(), typeof(ClassA));
TypeDescriptor.AddProvider(new MyTypeDescriptionProvider<ClassB>(), typeof(ClassB));

等等

于 2009-10-06T00:02:02.513 回答