マーティのソリューション (機能する) をさらに一歩進めて、使いやすくするための一般的な方法を次に示します。
public static U MapValidValues<U, T>(T source, U destination)
{
// Go through all fields of source, if a value is not null, overwrite value on destination field.
foreach (var propertyName in source.GetType().GetProperties().Where(p => !p.PropertyType.IsGenericType).Select(p => p.Name))
{
var value = source.GetType().GetProperty(propertyName).GetValue(source, null);
if (value != null && (value.GetType() != typeof(DateTime) || (value.GetType() == typeof(DateTime) && (DateTime)value != DateTime.MinValue)))
{
destination.GetType().GetProperty(propertyName).SetValue(destination, value, null);
}
}
return destination;
}
使用法:
class Person
{
public string Name { get; set; }
public int? Age { get; set; }
public string Role { get; set; }
}
Person person = new Person() { Name = "John", Age = 21, Role = "Employee" };
Person updatePerson = new Person() { Role = "Manager" };
person = MapValidValues(updatePerson, person);