Could a bit of reflection help ?
This is a conversion from a VB project that does something very similar to what you are doing.
public static void CopyPropertyValuesByName<T, T2>(ref T ObjFrom, T2 oBJtO)
{
Type iTypeFrom = typeof(T);
Type iTypeTo = typeof(T2);
Reflection.PropertyInfo[] pFrom = iTypeFrom.GetProperties();
Reflection.PropertyInfo[] pTo = iTypeTo.GetProperties();
PropertyInfo piTo = default(PropertyInfo);
const BindingFlags FLAGS = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
foreach (PropertyInfo piFrom in pFrom) {
piTo = iTypeTo.GetProperty(piFrom.Name, FLAGS);
if (piTo != null && !DBNull.Value.Equals(piFrom.Name)) {
if (piTo.CanWrite && piFrom.CanRead) {
piTo.SetValue(oBJtO, piFrom.GetValue(ObjFrom, null), null);
}
}
}
}