samedi 17 novembre 2018

How to get the values of properties for a list in a reflected object

If I have an object like this :

public class WorkTimeRegulation:BusinessObject
    {
        public WorkTimeRegulation(string name) : base()
        {
            _assignedWorkTimes = new List<WorkTime>();
        }
        public string Name { get; set; }
        private List<WorkTime> _assignedWorkTimes { get; set; }
        public virtual IEnumerable<WorkTime> AssignedWorkTimes { get => _assignedWorkTimes; }
   }


I use the following method to make a reflection on WorkTimeRegulation, Now how to get the values of the properties from the reflected object?

 public static void GetMyProperties(object obj)
        {
            List<Guid> relatedObjects = new List<Guid>();
            foreach (PropertyInfo pinfo in obj.GetType().GetProperties())
            {
                var getMethod = pinfo.GetGetMethod();
                if (getMethod.ReturnType.IsGenericType)
                {
                    var res = getMethod.Invoke(obj, null);

                    if (res != null)
                    {
                      //Here I want to get the values of WorkTime properties
                    }
                }
            }
        }





Aucun commentaire:

Enregistrer un commentaire