The goal is to make a string that represents the fields and they can be separated by any characters but the order of the fields is important.
public class TransactionContext
{
public string BranchId { get; set; }
public string AccountId { get; set; }
public int UserId { get; set; }
public int CheckNum { get; set; }
public int NumberOfRecords { get; set; }
public string StartNextId { get; set; }
public string StartPreviousId { get; set; }
public string NextId { get; set; }
public string PreviousId { get; set; }
public int RecordCount { get; set; }
public string ToPageContextString(string separator)
{
return BranchId + separator +
AccountId + separator +
UserId + separator +
CheckNum + separator+
NumberOfRecords + separator +
StartNextId + separator +
StartPreviousId + separator +
NextId + separator +
PreviousId + separator +
RecordCount + separator;
}
}
I tried using StringBuilder for a better solution and I get the following.
public string ToPageContextString(string separator)
{
var contextBuilder = new StringBuilder()
return contextBuilder.Append(BranchId + separator)
.Append(AccountId + separator)
.Append(UserId + separator)
.Append(CheckNum + separator)
...
.ToString();
}
My last aproach would be using reflection and a custom order attribute like suggested here Get properties in order of declaration using reflection.
Is they a more elegant solution ?