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?
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.
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.
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. :(
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With