views:

250

answers:

0

I'm trying to enable user of my application to add their own custom properties to objects. Classes which the user can add properties too implement the interface IUserPropertyHost, which is shown below. UserPropertyHostType is an enumeration that identifies a specific group of custom properties to apply to the object.

public interface IUserPropertyHost
{
    void SetUserPropertyValue(string name, object value);
    object GetUserPropertyValue(string name);
    void ClearUserProperty(string name);

    UserPropertyHostType UserPropHostType
    {
        get;
    }
}

I have one class working by implementing ICustomTypeDescriptor. Here's what my GetProperties() method looks like

public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
    //gets the base properties  (omits custom properties
    PropertyDescriptorCollection defaultProperties = TypeDescriptor.GetProperties(this, attributes, true);

    //gets the custom properties from the project based on the 
    //UserPropHostType property of the object
   Informa.RhInforma.PlugIn.RhInformaPlugin plugin = (RhInformaPlugin)RhUtil.GetPlugInInstance();
    Project proj = plugin.Project;

    //gets the custom properties from the project
    List<PropertyDescriptor> customProperties = proj.GetUserProperties(this.UserPropHostType);

    //unions the custom properties with the default properties
    List<PropertyDescriptor> fullList = new List<PropertyDescriptor>();
    fullList.AddRange(customProperties);

    foreach (PropertyDescriptor prop in defaultProperties)
    {
        fullList.Add(prop);
    }
    return new PropertyDescriptorCollection(fullList.ToArray());
}

Yet now I want to create a generic custom TypeDescriptorProvider and TypeDescriptor that will work with any IUserPropertyHost rather then implementing ICustomTypeDescriptor in every class. So here's my TypeDescriptionProvider class.

class InfTypeDescriptionProvider:TypeDescriptionProvider
{
    public InfTypeDescriptionProvider():base()
    {
    }

    public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
    {
         //instance is always null here???
    }
}

My problem is my CustomTypeDescriptor needs to know the UserPropertyHostType property of each object in order to determine what group of custom properties to use. I've added a [TypeDescriptionProvider(typeof(InfTypeDescriptionProvider))] attribute to one of my classes.

Yet when I try to show the properties of an instance in a property grid the instance parameter of the InfTypeDescriptionProvider.GetTypeDescriptor(Type type, Object instance) method is always null, where the type parameter is set to the correct type. Why would this parameter be null?