2013-07-19 19 views
6

我有一個很難通過TypeDescriptor獲取有關對象的索引信息 - 只是可以肯定,我的意思是這樣的事情:對象的索引器是否可以通過其TypeDescriptor以某種方式訪問​​?

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

既然你可以在WPF影響綁定與定製Typedescriptors和因爲你可以綁定到WPF中的索引器(例如{Binding [12]),我想知道索引器上的信息是否也可以通過類型描述符獲得。 那麼,信息隱藏的地方,如果它不隱藏,WPF綁定對索引器是如何工作的?

回答

4

簡短的回答,沒有的 - 你不能在索引通過TypeDescriptor

更長的答案讓 - 爲什麼你不能 - 在TypeDescriptor混亂鄰班的深處內心深處,有反射呼叫以聚合GetProperties呼叫的屬性。在有這樣的代碼:

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

有0指標參數進行檢查的重要組成部分 - 它是否有一個索引,它跳過它。 :(

+0

悲傷......我想綁定到索引器必須是某種形式的破解,因爲類型描述符不能給出信息 – flq

+0

@flq可能 - 我必須去挖掘以確認,但它有理由 – JerKimball

相關問題