如果该对象是使用反射位于列表内的对象的属性,则如何设置该对象的属性及其值 [英] 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

查看:35
本文介绍了如果该对象是使用反射位于列表内的对象的属性,则如何设置该对象的属性及其值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前也遇到过类似的问题,但这个问题需要不同的解决方案.

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.

如果两个对象的属性相同,我需要将模型对象属性的值设置为来自服务List 的属性值.

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.

这是一个模型:

public class MyModel{

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

ObjectAEntity 有一个名为SalesAmount"的属性

ObjectAEntity has a property called "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 的反射 在以下方法中:

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?

推荐答案

要实现此目的,您将尝试通过存储在当前 TicketReportPropertyEntity.ReportPropertyEntity 中的 string 值获取属性.属性名称.由于您已经有很多这样的设置,所以只需要多几行代码.

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

这篇关于如果该对象是使用反射位于列表内的对象的属性,则如何设置该对象的属性及其值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆