2010-07-05 136 views
3

在我的问题上,我想通过反射来检索一些值。 现在我想要设置值的对象感谢反射。是否可以从PropertyInfo获取“对象”?

我想这样写:

private void AppliquerColonnesPersonnalisation(Control control, Propriete propriete, PropertyInfo Info) 
     { 
      UltraGrid grille = (UltraGrid)control; 
      SortedList<int,string> sortedOrderedColumns = new SortedList<int,string>(); 

      if (grille != null) 
      { 
       // I want to write MapPropertyInfo method 
       ColumnsCollection cols = MapPropertyInfo(Info); 

的PropertyInfo包含一个类型ColumnsCollection的。我只是想将我的PropertyInfo“映射”到一个对象以定义一些属性后:例如:

cols[prop.Nom].Hidden = false; 

是否有可能?

最好的问候,

弗洛里安

编辑:我试过GenericTypeTea解决方案,但我有一些问题。在这里我的代码片段:

 private void AppliquerColonnesPersonnalisation(Control control, Propriete propriete, PropertyInfo Info) 
    { 
     UltraGrid grille = (UltraGrid)control; 
     ColumnsCollection c = grille.DisplayLayout.Bands[0].Columns; 

        // Throw a not match System.Reflection.TargetException 
     ColumnsCollection test = Info.GetValue(c,null) as ColumnsCollection; 
     SortedList<int,string> sortedOrderedColumns = new SortedList<int,string>(); 

但TargetException抛出

+0

你能告诉我们你得到'Info'的代码吗? – GenericTypeTea 2010-07-05 14:38:38

回答

2

所以,你已经有了一个PropertyInfo对象,它是ColumnsCollection型的?

你可以得到它,使用下面的代码进行修改:

var original = GetYourObject(); 
PropertyInfo Info = GetYourPropertyInfo(original); 
ColumnsCollection collection = Info.GetValue(original) as ColumnsCollection; 

基本上,你只需要通过你的原始对象放回PropertyInfo的GetValue方法将返回你的对象。只要将其作为ColumnsCollection进行投射,就应该进行排序。

UPDATE:

根据您的更新,你应该这样做:

object original = grille.DisplayLayout.Bands[0]; 
PropertyInfo info = original.GetProperty("Columns"); 

ColumnsCollection test = info.GetValue(original, null) as ColumnsCollection; 

你必须让你的InfoPropertyInfo从不同类型的对象。尽管我认为我们正在解决错误的问题。我不明白你想达到什么目的。为什么不直接修改grille.DisplayLayout.Bands[0].Columns

+0

好吧,我正在尝试...... – Florian 2010-07-05 13:38:17

+0

@florian - 任何喜悦? – GenericTypeTea 2010-07-05 13:51:22

+0

我正在编辑答案 – Florian 2010-07-05 14:28:08