jeudi 9 juillet 2020

Updating entity property values through reflection

I have entity class structure like this as below, having json columns that are related to classes and i am in the process of updating entity with some of the values.

[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "ORM", Scope = "module")]
public class DesignProject : PatchEntityProperties
{
    [Key, GraphQLNonNullType]
    public string ProjectNumber { get; set; }
    public string Name { get; set; }
    [Column(TypeName = "jsonb")]
    public ProjectSectionStatus SectionStatuses { get; set; } = new ProjectSectionStatus();
}

and then the ProjectSectionStatus class looks like as below

public class ProjectSectionStatus
{
    public Guid Id { get; set; } = Guid.NewGuid();
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public ProjectSectionStage ExecutiveSummarySectionStatus { get; set; } = ProjectSectionStage.NOT_STARTED;
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public ProjectSectionStage CodesAndGuidelinesSectionStatus { get; set; } = ProjectSectionStage.NOT_STARTED;
    public string CodesAndGuidelinesSectionNotesHTML { get; set; } = "";
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public ProjectSectionStage AirSystemsSectionStatus { get; set; } = ProjectSectionStage.NOT_STARTED;
    public string AirSystemsSectionNotesHTML { get; set; } = "";
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public ProjectSectionStage ExhaustEquipmentSectionStatus { get; set; } = ProjectSectionStage.NOT_STARTED;
    public string ExhaustEquipmentSectionNotesHTML { get; set; } = "";
    ....
    .....
    .....
}

below is where i am updating some of the properties in section statuses for targetdesignproject

 var targetDesignProject = this._dbContext.DesignProjects.Where(a => a.ProjectNumber == targetProjectNumber).SingleOrDefault();
 targetDesignProject.SectionStatuses.AirSystemsSectionStatus = Entities.Enums.ProjectSectionStage.INCOMPLETE;
 targetDesignProject.SectionStatuses.CodesAndGuidelinesSectionStatus = Entities.Enums.ProjectSectionStage.INCOMPLETE;
 targetDesignProject.SectionStatuses.ExecutiveSummarySectionStatus = Entities.Enums.ProjectSectionStage.INCOMPLETE;
 targetDesignProject.SectionStatuses.ExhaustEquipmentSectionStatus = Entities.Enums.ProjectSectionStage.INCOMPLETE;
 targetDesignProject.SectionStatuses.InfiltrationSectionStatus = Entities.Enums.ProjectSectionStage.INCOMPLETE;
        ......
        ......

there are 10 or more similar status properties that i need to update one by one with INCOMPLETE enum status so i have tried below approach using reflection method to update all at once but stuck at setting the value

 PropertyInfo[] properties = targetDesignProject.SectionStatuses.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            
 foreach (PropertyInfo property in properties)
 {
    var propValue = property.GetValue(targetDesignProject.SectionStatuses);

    if (propValue is ProjectSectionStage)
    {
       property.SetValue() // not sure how to update here 
    }
 }

Is there any way to update these statuses using reflection with Incomplete enum status. Could any one please let us know idea in suggestions how to update these using reflection that would be very grateful to me.

thanks in advance





Aucun commentaire:

Enregistrer un commentaire