あるオブジェクトの多くのプロパティを別のオブジェクトから更新しようとしていますが、この同じコードを何度も繰り返すことになります (Name と LastName の例を示していますが、同様のコードを持つ他の 15 のプロパティがあります)。
ただし、すべてのプロパティではないことに注意することが重要であるため、やみくもにすべてをコピーすることはできません。
public class Person
{
public bool UpdateFrom(Person otherPerson)
{
if (!String.IsNullOrEmpty(otherPerson.Name))
{
if (Name!= otherPerson.Name)
{
change = true;
Name = otherPerson.Name;
}
}
if (!String.IsNullOrEmpty(otherPerson.LastName))
{
if (LastName!= otherPerson.LastName)
{
change = true;
LastName = otherPerson.LastName;
}
}
return change;
}
}
このコードを書くためのよりエレガントな方法はありますか?