I've added functionality to my project that allows the users to add their own custom properties to objects. I've created my own custom TypeDescriptor, PropertyDescriptor and TypeDescriptorProviders etc.. etc.. to do this.
Here's my problem. Right now I have it all working, but had to create a separate TypeDescriptionProvider for each object object type that can have the custom properties. Here's what my TypeDescriptionProviders look like
//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);
}
}
So each of my custom TypeDescriptionProviders calls the base(TypeDescriptionProvider parent) base constructor by passing it the default TypeDescriptionProvider of a specific type.
The GetTypeDescriptor() method calls base.GetTypeDescriptor() to get the default descriptor which is then used by my custom type descriptor to add on the custom properties.
Is there some way to combine these into a single generic custom TypeDescriptionProvider that has the same functionality, but is not tied to a specific type? Can I skip providing the parent TypeDescriptionProvider in the constructor but later set it in the GetTypeDescriptor() method when I know specifically what type of object is being queried? Or is there some other way of getting the default descriptor of a type other then calling the base.GetTypeDescriptor(Type t,object ins) method?
This generic class should do what you want :