![](/img/trans.png)
[英]How can I avoid creating multiple use-case-specific adapter classes for my business logic classes?
[英]How can I avoid having a separate custom TypeDescriptorProvider for each of my classes?
我在项目中添加了一些功能,允许用户将自己的自定义属性添加到对象中。 我已经创建了自己的自定义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);
}
}
因此,我的每个自定义TypeDescriptionProvide都通过向其传递特定类型的默认TypeDescriptionProvider来调用基本(TypeDescriptionProvider父)基本构造函数。
GetTypeDescriptor()方法调用base.GetTypeDescriptor()来获取默认描述符,然后由我的自定义类型描述符用于添加自定义属性。
有没有办法将这些组合成一个具有相同功能但不依赖于特定类型的通用自定义TypeDescriptionProvider ? 我可以跳过在构造函数中提供父TypeDescriptionProvider但稍后在GetTypeDescriptor()方法中设置它时,我具体知道正在查询的对象类型是什么? 或者是否有其他方法获取类型的默认描述符,然后调用base.GetTypeDescriptor(Type t,object ins)方法?
这个泛型类应该做你想要的:
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));
等等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.