如何设置对象的属性和它的值,如果该对象是对象的属性是使用思考名单内(How to set Objec

2019-09-28 01:35发布

我以前也有过类似的问题,但是这一次将需要不同的解决方案。

我有我的模型对象,并在我的服务对象。

我需要模型的对象属性的值设置为从服务的未来属性的值List<TicketReportPropertyEntity>如果两个对象的属性是相同的。

这是一个型号:

public class MyModel{

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

ObjectAEntity有一个名为“SalesAmount两个”属性

这是一个服务:

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

所有属性,它们的值和段(ReportGroup)在屏幕上它们属于(ObjectAData到LeftSection和ObjectBData到RightSection)我使用从反射获取List<TicketReportPropertyEntity>在下面的方法:

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

我怎样才能做这样的事情?

Answer 1:

要做到这一点,你会试图通过获取属性string存储在当前值TicketReportPropertyEntity.ReportPropertyEntity.PropertyName 。 既然你已经有很多这样设置的,只需要代码的一对夫妇更行。

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


文章来源: 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
标签: c# reflection