Home > Software engineering >  Using Reflection to Iterate and update value of an object for specific property types
Using Reflection to Iterate and update value of an object for specific property types

Time:12-18

I have an object, employeesOfTheMonth of type EmployeesOfTheMonth, which contains a number of properties of types Employee as well as List<Employee>. In some instances, the value of a given employee's JobTitleUpdated field contains an updated job title; in other instances, the JobTitleUpdated field is null.

    public class Employee
    {
        public int EmployeeId {get; set; }
        public string EmployeeName { get; set;}
        public string? JobTitle { get; set; }
        public string? JobTitleUpdated { get; set;}
        public decimal Salary { get; set; }
        public byte[] Photo { get; set; }
    }

    public class EmployeesOfTheMonth
    {   
        public string Company
        public Employee EmployeeOfTheMonthLocation1 {get; set;}
        public Employee EmployeeOfTheMonthLocation2 {get ;set;}
        public Employee EmployeeOfTheMonthLocation3 {get; set;}
        public List<Employee> EmployeesOfPriorMonthsLocation1 {get; set;}
        public List<Employee> EmployeesOfPriorMonthsLocation2 {get; set;}
        public List<Employee> EmployeesOfPriorMonthsLocation3 {get; set;}
        public DateTime ModifiedDate { get; set; }
        [... additional properties of various types snipped...]
    }

Using Reflection, I would like to iterate the properties of employeesOfTheMonth. If the property is of type Employee and if the JobTitleUpdated property of a given Employee contains data, I need to update the value of the JobTitle of the Employee with the value of the JobTitleUpdated property. In the case of List<Employee>, I need to iterate through that list and apply the same logic, namely conditionally updating the JobTitle with the JobTitleUpdated property.

Can this be done via Reflection?

CodePudding user response:

Yes, you can use reflection to iterate through the properties of employeesOfTheMonth and update the JobTitle property of the Employee objects if the JobTitleUpdated property contains data.

Here is an example of how you can do this using reflection:

Type type = employeesOfTheMonth.GetType();

foreach (PropertyInfo property in type.GetProperties())
{
    if (property.PropertyType == typeof(Employee))
    {
        // Get the value of the property
        Employee employee = (Employee)property.GetValue(employeesOfTheMonth);

        // Update the JobTitle if JobTitleUpdated is not null
        if (employee.JobTitleUpdated != null)
        {
            employee.JobTitle = employee.JobTitleUpdated;
        }
    }
    else if (property.PropertyType == typeof(List<Employee>))
    {
        // Get the value of the property
        List<Employee> employees = (List<Employee>)property.GetValue(employeesOfTheMonth);

        // Iterate through the list and update the JobTitle if JobTitleUpdated is not null
        foreach (Employee employee in employees)
        {
            if (employee.JobTitleUpdated != null)
            {
                employee.JobTitle = employee.JobTitleUpdated;
            }
        }
    }
}

This code will iterate through the properties of employeesOfTheMonth and check if the property is of type Employee or List. If the property is of type Employee, it will update the JobTitle with the value of JobTitleUpdated if JobTitleUpdated is not null. If the property is of type List, it will iterate through the list and apply the same logic to each Employee object in the list.

  • Related