【问题标题】:Updating entity property values through reflection通过反射更新实体属性值
【发布时间】:2020-10-30 15:57:18
【问题描述】:

我有如下的实体类结构,有与类相关的 json 列,我正在用一些值更新实体。

[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();
}

然后 ProjectSectionStatus 类如下所示

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; } = "";
    ....
    .....
    .....
}

下面是我更新 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;
        ......
        ......

我需要使用INCOMPLETE 枚举状态一一更新 10 个或更多类似的状态属性,所以我尝试了以下方法,使用反射方法一次更新,但停留在设置值

 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 
    }
 }

有没有办法通过Incomplete 枚举状态使用反射来更新这些状态。任何人都可以让我们知道如何使用反射更新这些建议的想法,非常感谢我。

提前致谢

更新:

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
      }
 }

【问题讨论】:

  • 谁能指出我正确的方向,为什么我会收到这个错误
  • 还有一个选项叫Automapper,它使用底层下的反射。您可以使用此库将您的对象属性映射到另一个对象属性。 github.com/AutoMapper/AutoMapper

标签: c# .net generics reflection properties


【解决方案1】:

我已经用下面的代码解决了这个问题

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
      }
 }

【讨论】:

    【解决方案2】:

    你可以的

    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)
        }
     }
    

    另外,如果您需要更多文档,可以参考这些页面:

    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___

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多