我怎样才能避免我的每个类别的单独定制TypeDescriptionProvider?
-
19-09-2019 - |
题
我添加功能到我的项目,允许用户自己定制属性添加到对象。我创建自己的自定义的 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 强>调用的碱(TypeDescriptionProvider亲本)强>基构造。
的 GetTypeDescriptor()方式的呼叫的 base.GetTypeDescriptor()以得到默认描述符,然后将其使用的我的自定义类型描述符来对自定义属性添加。
是否有某种方式将这些组合成一个通用的自定义的 TypeDescriptionProvider 具有相同的功能,但不依赖于特定类型的?我可以跳过提供母体的 TypeDescriptionProvider 强>在构造函数中,但后来将其设置在 GetTypeDescriptor()方式,当我知道具体被查询的对象的类型?或者是有让其他然后调用的 base.GetTypeDescriptor(T型,对象项)的类型的默认描述符的一些其它方式方式?
没有正确的解决方案
其他提示
这泛型类应该做你想要什么:
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);
}
}
我已经在过去通过使用通用型处理这样的:
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));
等