How to set Object's properties and its value if that Object is a property of Object that is inside of the List using Reflections

馋奶兔 提交于 2019-12-24 07:34:22

问题


I had a similar question before, but this one will need a different solution.

I have object on my Model and object on my service.

I need to set value of Model's object property to a value of properties coming from the service's List<TicketReportPropertyEntity> if both objects' properties are the same.

This is a Model:

public class MyModel{

     public ObjectAEntity ObjectAData { get; set; }
     public ObjectBEntity ObjectBData { get; set; }
}

ObjectAEntity has a property called "SalesAmount"

This is a service:

public class MyScreenClass
{
     public List<TicketReportPropertyEntity> TicketReportPropertyEntities { get; set; } 
}

public class TicketReportPropertyEntity
{
    public decimal Amount{get;set;}
    public ReportPropertyEntity ReportProperty {get;set;}
} 

public class ReportPropertyEntity
{
    public string ReportGroup { get; set; }        
    public string PropertyName { get; set; }
}

All the properties, their values and which section(ReportGroup) on the screen they belong to (ObjectAData to the LeftSection and ObjectBData to the RightSection) I'm getting using a reflection from List<TicketReportPropertyEntity> in the following method:

private void SetValues(MyModel m, ObjectAEntity bo, object objectType)
{
    string leftSection = "LeftSection";
    string rightSection = "RightSection";


    m.ObjectAData.SaleAmount = bo.ObjectAData.SaleAmount;
    foreach (var ticketReportEntity in mol.TicketReportPropertyEntities)
    {
        var type = ticketReportEntity.GetType();
        PropertyInfo reportProperty = type.GetProperty("ReportProperty");
        PropertyInfo reportPropertyName = typeof(ReportPropertyEntity).GetProperty("PropertyName");
        PropertyInfo reportPropertyReportGroup = typeof(ReportPropertyEntity).GetProperty("ReportGroup");
        PropertyInfo amountProperty = type.GetProperty("Amount");
        ReportPropertyEntity reportPropertyValue = (ReportPropertyEntity)reportProperty.GetValue(ticketReportEntity, null);
        string reportPropertyNameValue = (string)reportPropertyName.GetValue(reportPropertyValue, null);
        decimal value = (decimal)amountProperty.GetValue(ticketReportEntity, null);


//here I need to see if Model's object has the same property as in `ReportProperty` class. 

    //here I need to find out if the ObjectAEntity has the same property as ReportProperty 

    if (has)
    {
        //need to set the value of the Model's `ObjectAEntity` property 
    }
}

How can I do something like that?


回答1:


To accomplish this, you would attempt to get the property by the string value stored in the current TicketReportPropertyEntity.ReportPropertyEntity.PropertyName. Since you already have a lot of this setup, it only takes a couple more lines of code.

//here I need to find out if the ObjectAEntity has the same property as ReportProperty

//Attempt to grab the PropertyInfo that you want to set
var objectAEntityReportProperty = bo.GetType().GetProperty(reportPropertyNameValue);

//If it is not null, you have found a match
var has = objectAEntityReportProperty != null;
if (has)
{
    //need to set the value of the Model's `ObjectAEntity` property
    //Then, set the value
    objectAEntityReportProperty.SetValue(bo, ticketReportEntity.Amount);
}


来源:https://stackoverflow.com/questions/57222034/how-to-set-objects-properties-and-its-value-if-that-object-is-a-property-of-obj

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!