vendredi 21 avril 2017

How to get value of property in object inside a List

I need to work with properties of objects contained in a List of variable Types, for which i have the following approach using Reflection:

        private System.Data.DataTable ListToDataTable<T>(List<T> L) {

           PropertyInfo[] PIs = typeof(T).GetProperties();

           // Iterating through elements of the List, which are instances of T
           for(int x=0;x<L.Count;x++) {

              Console.WriteLine(PI.GetValue(L[x]).ToString());

           }

        }

I receive a System.Reflection.TargetParameterCountException when executions reaches to try an get the value of said property, it seems this not the appropiate way to get such a property and i know not other ways to do it than Reflection.

Is there another way to do it? Or, which is the proper way to achieve it using Reflection?

Casting, converting or declaring as T before PI.GetValue throws the same Exception.

Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire