我以前也有过类似的问题,但是这一次将需要不同的解决方案。
我有我的模型对象,并在我的服务对象。
我需要模型的对象属性的值设置为从服务的未来属性的值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
}
}
我怎样才能做这样的事情?