Bonjour @ tous,
Je fais de la réflexion, et je crois que je vais devenir fou !
J'ai un objet qui contient des propriétés et une liste d'autres objets.
j'arrive à récupérer tout ce qui est du type :
Avec cette fonction :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 obj1.libelle obj1.obj2 obj1.obj3[1].libelle (grace au "if ((val is IEnumerable) && (idx>=0)) { // Pour les listes"... )
Par contre, je n'arrive pas à récupérer :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 /// <summary>Récupère les meta-information de la propriétés "pPropertyName".</summary> /// <returns>Item1=PropertyInfo, Item2=ObjetParent, Item3=ValeurDeLaPropriété</returns> public static Tuple<PropertyInfo, object, object> GetPiByName(object pObj, string pPropertyName) { PropertyInfo pi = null; object val = pObj; object valParent = null; Type currentType = val.GetType(); var idx = -1; foreach (string propertyName in pPropertyName.Split('.')) { if ((val is IEnumerable) && (idx>=0)) { // Pour les listes pi = pi.PropertyType.GetGenericArguments()[0].GetProperty(propertyName); currentType = pi.PropertyType; valParent = ((IList)val)[idx]; val = pi.GetValue(valParent, null); idx = -1; continue; } // */ var posi = propertyName.IndexOf('['); if ((posi>0) && int.TryParse(propertyName.Substring(posi + 1, propertyName.Length - propertyName.IndexOf(']')), out idx)) { pi = currentType.GetProperty(propertyName.Substring(0, posi)); } else pi = currentType.GetProperty(propertyName); currentType = pi.PropertyType; valParent = val; val = pi.GetValue(val, null); } return new Tuple<PropertyInfo, object, object>(pi, valParent, val); }
tout court. C'est juste une List<obj3>, j'aimerais retourner l'objet racine, et pas une de ses sous propriétés
Code : Sélectionner tout - Visualiser dans une fenêtre à part obj1.obj3[1]
Je ne trouve pas la méthode pour me renvoyer le PropertyInfo d'un élément de liste par son index.
J'ai modifié la méthode et j'y suis presque, mais il me manque le PI
Est ce que quelqu'un a une idée ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public static Tuple<PropertyInfo, object, object> GetPiByName(object pObj, string pPropertyName) { PropertyInfo pi = null; object val = pObj; object valParent = null; Type currentType = val.GetType(); var idx = -1; foreach (string propertyName in pPropertyName.Split('.')) { var posi = propertyName.IndexOf('['); if ((posi>0) && int.TryParse(propertyName.Substring(posi + 1, propertyName.Length - propertyName.IndexOf(']')), out idx)) { pi = currentType.GetProperty(propertyName.Substring(0, posi)); currentType = pi.PropertyType; valParent = val; val = pi.GetValue(val, null); if ((val is IEnumerable) && (idx >= 0)) { // Pour les listes valParent = val; val = ((IList)val)[idx]; currentType = val.GetType(); pi = ??? Je met quoi là ??? idx = -1; continue; } // */ } else pi = currentType.GetProperty(propertyName); currentType = pi.PropertyType; valParent = val; val = pi.GetValue(val, null); } return new Tuple<PropertyInfo, object, object>(pi, valParent, val); }
Merci d'avance.
Partager