using System.Collections.Generic;
public static void Main()
DTO dto = new DTO { EngId = 1 };
SetDtoFields(dto, rd.EngDetailBitsList(dto.EngId));
SetDtoFields(dto, rd.EngDetailDateTimesList(dto.EngId));
SetDtoFields(dto, rd.EngDetailVarCharsList(dto.EngId));
SetDtoFields(dto, rd.EngDetailVarCharMaxesList(dto.EngId));
static void SetDtoFields<T>(object targetDto, IEnumerable<T> fields)
Type fieldType = typeof(T);
var fieldNameProp = fieldType.GetProperty("ShortDescript");
if (fieldNameProp == null || !fieldNameProp.CanRead)
var dataValProp = fieldType.GetProperty("DataValue");
if (dataValProp == null || !dataValProp.CanRead)
Type targetType = targetDto.GetType();
foreach (T field in fields)
var propToSet = targetType.GetProperty((string)fieldNameProp.GetValue(field),
BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase );
if (propToSet != null && propToSet.CanWrite &&
propToSet.PropertyType.IsAssignableFrom(dataValProp.PropertyType))
propToSet.SetValue(targetDto, dataValProp.GetValue(field));
static void Dump(object obj)
foreach (PropertyInfo prop in obj.GetType().GetProperties())
Console.WriteLine(prop.Name + ": " + prop.GetValue(obj));
public int EngId { get; set; }
public string Name { get; set; }
public DateTime Birthdate { get; set; }
public DateTime MarriedDate { get; set; }
public int HeightInInches { get; set; }
public bool LikesSpicyFood { get; set; }
public bool FriesWithThat { get; set; }
public string FavoriteColor { get; set; }
public int ShoeSize { get; set; }
public string ShortDescript { get; set; }
public bool DataValue { get; set; }
public string ShortDescript { get; set; }
public DateTime DataValue { get; set; }
public string ShortDescript { get; set; }
public string DataValue { get; set; }
public string ShortDescript { get; set; }
public int DataValue { get; set; }
public List<BoolField> EngDetailBitsList(int id)
return new List<BoolField>
new BoolField { ShortDescript = "LikesSpicyFood" , DataValue = false },
new BoolField { ShortDescript = "FriesWithThat" , DataValue = true },
public List<DateTimeField> EngDetailDateTimesList(int id)
return new List<DateTimeField>
new DateTimeField { ShortDescript = "Birthdate" , DataValue = new DateTime(1969, 4, 15) },
new DateTimeField { ShortDescript = "MarriedDate" , DataValue = new DateTime(1995, 6, 21) },
public List<StringField> EngDetailVarCharsList(int id)
return new List<StringField>
new StringField { ShortDescript = "Name" , DataValue = "Joe Schmoe" },
new StringField { ShortDescript = "FavoriteColor" , DataValue = "Purple" },
public List<IntField> EngDetailVarCharMaxesList(int id)
return new List<IntField>
new IntField { ShortDescript = "HeightInInches" , DataValue = 70 },
new IntField { ShortDescript = "ShoeSize" , DataValue = 12 },