如何重构呢?
|
我试图重构这个
class AClass
{
string Property1 { get; set; }
string Property2 { get; set; }
string Property3 { get; set; }
void AMethod(AClass other)
{
if(String.IsNullOrEmpty(this.Property1))
{
this.Property1 = other.Property1;
}
if(String.IsNullOrEmpty(this.Property2))
{
this.Property2 = other.Property2;
}
if(String.IsNullOrEmpty(this.Property3))
{
this.Property3 = other.Property3;
}
}
}
我唯一能想到的是
private string GetFirstNotNullOrEmpty(string first, string second)
{
if (String.IsNullOrEmpty(first))
{
return second;
}
return first;
}
和
this.Property1 = GetFirstNotNullOrEmpty(this.Property1, other.Property1);
这并不完全等效,但是可以胜任。有没有更好的方法来重构它?
没有找到相关结果
已邀请:
6 个回复
缝皋
吞睫素
唤副埂侧壬
禽兢玫坞劲
陷牡彭拈峰
它将被这样称呼:
如果这些不是C#属性,那会更好。使用公共字段,我可以传递引用,并将getter和setter都放在一个参数中。
系漏