如何重构呢?

| 我试图重构这个
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);
这并不完全等效,但是可以胜任。有没有更好的方法来重构它?     
已邀请:
如果要对该类的N个字符串属性执行此操作,则应使用Reflection来实现。 更新资料 都是关于“编码”的,对不对?它去了:
class SomeClass
{
    public string Property0 { get; set; }
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }
    public string Property4 { get; set; }
    public string Property5 { get; set; }
    public string Property6 { get; set; }
    public string Property7 { get; set; }
    public string Property8 { get; set; }
    public string Property9 { get; set; }

    public override string ToString()
    {
        //just to print out all properties and values
        foreach (PropertyInfo prop in typeof(SomeClass).GetProperties())
        {
            Console.WriteLine(prop.Name + \",\" + prop.PropertyType + \" = \" + prop.GetValue(this, null));
        }
        return base.ToString();
    }

    public void CopyStringPropertiesIfEmptyFrom(SomeClass SourceInstance)
    {
        foreach (PropertyInfo prop in typeof(SomeClass).GetProperties())
        {
            if (prop.PropertyType == typeof(System.String) && String.IsNullOrEmpty((string)prop.GetValue(this, null)))
            {
                prop.SetValue(this, prop.GetValue(SourceInstance, null), null);
            }
        }
    }

}
    
代替使用方法,您可以将if折叠为三元运算符:
this.Property1 = String.IsNullOrEmpty(this.Property1)? other.Property1 : this.Property1;
    
在属性本身中实现检查。
public class AClass
{
    string Property1 
    { 
        get { return _Property1; }
        set
        {
            if (String.IsNullOrEmpty(_Property1))
            {
                _Property1 = value
            }
        }
    }
    private string _Property1;


    void AMethod(AClass other)
    {
        this.Property1 = other.Property1;// Property can only be set once.
    }

}
    
如果可以避免,我不喜欢使用Reflection,所以我实际上很喜欢您在问题中建议的选项,但与Tesserex的回答略有不同:
private string GetFirstNotNullOrEmpty(string first, string second)
{
    return String.IsNullOrEmpty(first)) ? second : first;
}
    
我认为最好的解决方案是
private void SetFirstNotNullOrEmpty(string first, string second, Action<T> setter)
{
    if (String.IsNullOrEmpty(first))
    {
        setter(second);
    }
}
它将被这样称呼:
this.Property1 = GetFirstNotNullOrEmpty(this.Property1, other.Property1, i => this.Property1 = i);
如果这些不是C#属性,那会更好。使用公共字段,我可以传递引用,并将getter和setter都放在一个参数中。     
这里需要重构的第一件事是不直观的名称,例如Property1和AClass。为类和属性名称使用有意义的名称,以便它们清楚地反映意图。 OP可能希望我们专注于手头的问题,而不是这方面。     

要回复问题请先登录注册