简体   繁体   中英

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

Update:

foreach (PropertyInfo property in properties)
{
     var propValue = property.GetValue(targetDesignProject.SectionStatuses);

     if (propValue is ProjectSectionStage)
     {
           property.SetValue(targetDesignProject, ProjectSectionStage.INCOMPLETE, null); 
           // getting an error - Object does not match target type
      }
 }

I have solved this problem using below code

foreach (PropertyInfo property in properties)
{
     var propValue = property.GetValue(targetDesignProject.SectionStatuses);

     if (propValue is ProjectSectionStage)
     {
           property.SetValue(targetDesignProject.SectionStatuses, ProjectSectionStage.INCOMPLETE, null); 
           // getting an error - Object does not match target type
      }
 }

You can do

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(targetDesignProject.SectionStatuses, propValue)

       // You can do this if you want
       // var newPropValue = rojectSectionStage.INCOMPLETE;
       // property.SetValue(targetDesignProject.SectionStatuses, newPropValue)
    }
 }

Also, you can refer to these page if you want more documentation:

https://docs.microsoft.com/en-us/dotnet/api/system.reflection.propertyinfo.setvalue?view=netcore-3.1

https://docs.microsoft.com/en-us/dotnet/api/system.reflection.propertyinfo.setvalue?view=netcore-3.1#System_Reflection_PropertyInfo_SetValue_System_Object_System_Object_System_Object__ _

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM