大家好,感谢您的关注。多个LIST <>类型的C#FOREACH
我不认为这是可能的,但我想通过多个返回的LIST <>类型执行一个完全相同的FOREACH,而不必剪切和粘贴代码4次。的dto2,dto3,dto4的所有属性,并且返回dto5列表是相同的,除了DataValue,这对于每个不同的数据类型(INT,VARCHAR,布尔等)
var dto2 = rd.EngDetailBitsList(dto.EngId);
var dto3 = rd.EngDetailDateTimesList(dto.EngId);
var dto4 = rd.EngDetailVarCharsList(dto.EngId);
var dto5 = rd.EngDetailVarCharMaxesList(dto.EngId);
foreach (var x in dto2)
{
var propertyInfo = dto.GetType().GetProperty(x.ShortDescript,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
if (propertyInfo != null)
{
propertyInfo.SetValue(dto, x.DataValue);
}
}
foreach (var x in dto3)
{
var propertyInfo = dto.GetType().GetProperty(x.ShortDescript,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
if (propertyInfo != null)
{
propertyInfo.SetValue(dto, x.DataValue);
}
}
foreach (var x in dto4)
{
var propertyInfo = dto.GetType().GetProperty(x.ShortDescript,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
if (propertyInfo != null)
{
propertyInfo.SetValue(dto, x.DataValue);
}
}
foreach (var x in dto5)
{
var propertyInfo = dto.GetType().GetProperty(x.ShortDescript,
BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
if (propertyInfo != null)
{
propertyInfo.SetValue(dto, x.DataValue);
}
}
'dto2'''dto5'的类型是什么?他们是否使用'ShortDescription'和'DataValue'属性来实现具有公共基类的通用接口? – MarcinJuraszek 2014-11-25 02:05:47
'ShortDescript'可能会返回不同的值,对吧?所以你可以在'dto'上设置不同的属性。 – MarcinJuraszek 2014-11-25 02:30:24
@BrianRogers我不明白。对我来说,这是有道理的。即使列表中所有项目的类型相同,“ShortDescript”属性也可以返回指向“dto”上不同属性的不同“字符串”值。 – MarcinJuraszek 2014-11-25 02:37:34