c#构造函数调用toString

时间:2016-12-15 14:05:13

标签: c# constructor tostring

我有一个覆盖iComparable的对象,它也会覆盖toString()。

当调用构造函数时,它也调用toString(),但没有命中toString()方法中的任何断点。

当执行构造函数的每一行时,也会调用toString(),并且一旦DataSource属性被更新,它就会从数据库中获取数据并产生意外结果。

我已经通过每次调用的environment.stacktrace记录证明了这一点。

这是预期的行为,并且无论如何只在我明确地调用它而不是自动调用toString()时。

班级代码在

之下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Business.Data;

namespace PfWToEpayfactGIF
{
class PsPayChangeDetails : IComparable<PsPayChangeDetails>
{
  public DateTime EffectiveDate { get; set; }
  public String Code { get; set; }
  public string ChangeType { get; set; }

  public int DataSourceX { get; set; }

  private string gradeID;
  private string gradeSubCode;
  private string gradePercentage;
  private string IDField;
  private string salary;
  private string maxOTRateType;
  private string lWeightType;
  private string onTempPromotion;
  private string RTIHoursInd;
  private string paygroupID;
  private string classification;
  private string hoursPayable;
  private string workingPatternID;
  private string OSPSchemeNo;
  private string milestoneDate;
  private string employeeNo;


  public PsPayChangeDetails(string code, DateTime effectiveDate, string changeType, int groupSource)
  {
     EffectiveDate = effectiveDate;
     Code = code;
     ChangeType = changeType;
    DataSourceX = groupSource;
  }

  public void ReadLine(string[] line)
  {
     employeeNo = line[0] != string.Empty ? line[0] : employeeNo;
     try
     {
        if (Code == "E420")
        {
           gradeID = line[4] != string.Empty ? line[4] : gradeID;
           gradeSubCode = line[5] != string.Empty ? line[5] : gradeSubCode;
           gradePercentage = line[6] != string.Empty ? line[6] : gradePercentage;
           IDField = line[7] != string.Empty ? line[7] : IDField;
           salary = line[8] != string.Empty ? line[8] : salary;
           maxOTRateType = line[9] != string.Empty ? line[9] : maxOTRateType;
           lWeightType = line[10] != string.Empty ? line[10] : lWeightType;
           onTempPromotion = line[11] != string.Empty ? line[11] : onTempPromotion;
           RTIHoursInd = line[12] != string.Empty ? line[12] : RTIHoursInd;
        }
        else
        {

           paygroupID = line[4] != string.Empty ? line[4] : paygroupID;
           classification = line[5] != string.Empty ? line[5] : classification;
           hoursPayable = line[6] != string.Empty ? line[6] : hoursPayable;
           workingPatternID = line[7] != string.Empty ? line[7] : workingPatternID;
           OSPSchemeNo = line[8] != string.Empty ? line[8] : OSPSchemeNo;
           milestoneDate = line[9] != string.Empty ? line[9] : milestoneDate;
        }
     }
     catch (IndexOutOfRangeException)
     {
        //ignore the exception as its telling us we dont have all the fields which is fine.
     }

  }

  public override string ToString()
  {
     using (System.IO.StreamWriter write = new System.IO.StreamWriter(@"c:\temp\test.txt", true))
     {
        write.WriteLine(Environment.StackTrace);
     }
        string output = $"{employeeNo},{Code},{EffectiveDate.ToString("dd/MMM/yyyy")},{ChangeType},";
     if (Code == "E420")
     {
        output += $"{gradeID},{gradeSubCode},{gradePercentage},{IDField},{salary},{maxOTRateType},{lWeightType},{onTempPromotion},{RTIHoursInd}";
     }
     else
     {
        using (DataEntities db = new DataEntities(DataSourceX))
        {

           if (paygroupID == null)
           {
              string partTime = workingPatternID == "PT" ? "Y" : "N";
              paygroupID = db.PayGroupEESetting.Where(pge => pge.PartTimeInd == partTime && pge.EnteredDate == db.PayGroupEESetting.Where(pg => pg.PayGroup == pge.PayGroup && pg.EnteredDate <= EffectiveDate).Max(pg => pg.EnteredDate)).OrderBy(pge => pge.PayGroup).FirstOrDefault().PayGroup;
           }
           workingPatternID = workingPatternID == null ? "FT" : "PT";
           if (OSPSchemeNo == null)
           {
              OSPSchemeNo = db.OSPScheme.Min(o => o.SchemeNo).ToString();
           }
        }
        output += $"{paygroupID},{classification},{hoursPayable},{workingPatternID},{OSPSchemeNo},{milestoneDate}";
     }
     return output;
  }


  public int CompareTo(PsPayChangeDetails other)
  {
     if (this.Code == other.Code)
     {
        return this.EffectiveDate.CompareTo(other.EffectiveDate);
     }
     else
     {
        return this.Code.CompareTo(other.Code);
     }
  }
}
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

  

当执行构造函数的每一行时,也会调用toString()

我猜你知道这个,因为你正在逐步调试调试器中的代码。如果是这种情况,那么调试器正在对象上调用ToString以在某些网格或其他输出(局部变量,监视等)中表示它。由于调试器处于不同的进程中,因此可能无法遇到任何断点。

底线是 - 不要将ToString用于其他任何事情,只需将对象的当前状态表示为字符串变量。如果您需要加载相关对象,以特定方式格式化对象等,请执行ToString之外的