<T>类型的通用列表-获取集合中项目的索引?

问题描述

我创建了一个 ObservableCollection T 方法,用于导出。现在,我需要从集合中读取所有数据,但还需要向 Dictionary 中添加值,其中 int 应该是集合中属性的索引。

为此,我自己找到了一个解决方案:

     private void Get_property_values<T>(ObservableCollection<T> observableCollection)
     {
       //Dictionary where I need to update values,Items are added to It in same method
       // - before reading collection values

       Dictionary<int,string> txt_with_indx = new Dictionary<int,string>(); 
      
       txt_with_indx.Add(0,"value_to_maybe_update"); //etc...
      
       var props = typeof(T).GetProperties();
       
       int indx = 0; 
       foreach (var list in observableCollection)
       {
          indx = 0;
    
          foreach (var prop in props)
          {
             string obs_txt = prop.GetValue(list,null).ToString().Trim() ?? string.Empty;
                     
             if (txt_with_indx[indx].Length < obs_txt.Length)
             {
                  txt_with_indx[indx] = obs_txt;
             }
             indx++;
         }
       }
     }

要获取属性的索引,我使用了变量“ indx ”。代码有效。

我的问题:使用T型泛型列表时,有什么比这合适,更优雅或更轻松的解决方案?

感谢您提前回答!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)