Is an object's indexer somehow accessible through its TypeDescriptor?

It's hard for me to get information about the object indexer via TypeDescriptor - just to be sure, I mean this thing:

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

Since you can influence the binding in WPF with the Typedescriptors setting, and since you can communicate with indexers in WPF (for example, {Binding [12] ), I was interested to find out if information about indexers is available through the type descriptor. So, where is the information hiding, and if it is not hiding there, how does WPF binding to indexers work?

+6
source share
1 answer

The short answer is no - you cannot get to indexers via TypeDescriptor

The longer answer — why you can't — deep down in the bowels of TypeDescriptor mess-o-classes, is a reflective call to aggregate properties for calling GetProperties . This code has:

 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); } } } 

An important part is checking for index parameters 0 - if it has an index, it skips it. :(

+4
source

Source: https://habr.com/ru/post/949838/


All Articles