Trivial implementation of ICustomTypeDescriptor in C#

It appears that the implementation of ICustomTypeDescriptor is an easy task.  Basically we can redirect all the ICustomTypeDescriptor method calls to TypeDescriptor class that provides static type information obtained from metadata or derive from CustomTypeDescriptor class. Below I provided the source code.

public class ViewModel : ICustomTypeDescriptor
{
        #region ICustomTypeDescriptor implementation

        AttributeCollection ICustomTypeDescriptor.GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        string ICustomTypeDescriptor.GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }

        string ICustomTypeDescriptor.GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }

        TypeConverter ICustomTypeDescriptor.GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
        {
            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(this, attributes, true);

            return props;
        }

        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
        {
            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(this, true);

            return props;
        }

        object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }
        
        #endregion ICustomTypeDescriptor implementation
}

2 Responses to Trivial implementation of ICustomTypeDescriptor in C#

  1. Tiks on December 4, 2015 at 3:18 PM:

    Is there a reason that in two of the methods you have done:

    PropertyDescriptorCollection props = TypeDescriptor.GetProperties();
    return props;

    Instead of just:

    return TypeDescriptor.GetProperties();

    ?

  2. superadmin on December 5, 2015 at 9:21 AM:

    No, I just wanted to see the value of ‘props’ in the debugger.

Leave a Reply

Your email address will not be published. Required fields are marked *