Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 616
flq Avatar asked Jul 19 '13 16:07

flq


People also ask

When to use indexers?

You typically use an indexer if the class represents a list, collection or array of objects. In your case, you could provide an indexer to provide index-based access to a teacher's students.

What is indexer vb net?

An indexer allows you to access a class instance in terms of a member array. An indexer declaration may include a set of attributes; a new modifier; a valid combination of the public, private, protected, and internal access modifiers; and one of the virtual, override, or abstract modifiers.


1 Answers

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. :(

like image 161
JerKimball Avatar answered Oct 13 '22 12:10

JerKimball