Search code examples
.netwpfdata-bindingtypedescriptor

Is the Indexer of an object somehow accessible through its TypeDescriptor?


I am having a hard time obtaining information about an object's indexer through the TypeDescriptor - just to be sure, I mean that kind of thing:

class ComponentWithIndexer
{
    public string this[int i]
    {
        get { return "hello"; }
    }
}

Since you can influence Binding in WPF with customizing Typedescriptors and since you can bind to indexers in WPF ( e.g. {Binding [12] ) I was wondering whether information on Indexers is also available through a Type descriptor. So, where does the info hide, and if it doesn't hide there, how does the WPF Binding against indexers work?


Solution

  • Short answer, no - you can't get at indexers via TypeDescriptor

    Longer answer - why you can't - deep down in the bowels of the TypeDescriptor mess-o-classes, there is the reflective call to aggregate properties for the GetProperties call. In there is this code:

    for (int i = 0; i < properties.Length; i++)
    {
        PropertyInfo propInfo = properties[i];
        if (propInfo.GetIndexParameters().Length <= 0)
        {
            MethodInfo getMethod = propInfo.GetGetMethod();
            MethodInfo setMethod = propInfo.GetSetMethod();
            string name = propInfo.Name;
            if (getMethod != null)
            {
                sourceArray[length++] = new ReflectPropertyDescriptor(type, name, propInfo.PropertyType, propInfo, getMethod, setMethod, null);
            }
        }
    }
    

    The important part there is the check for 0 index parameters - if it has an indexer, it skips it. :(