using System.Linq.Expressions;
public static void Main()
Console.WriteLine(item.OldToString());
Console.WriteLine(item.NewToString());
Console.WriteLine(item.OldToString() == item.NewToString());
public DateTime? MaxBulkCreationUtcDateTime { get; set; }
public DateTime? MinBulkCreationUtcDateTime { get; set; }
public int Limit { get; set; }
public bool IsUnmonitored { get; set; }
public bool IsAlerted { get; set; }
public string OldToString()
return $"MaxBulkCreationUtcDateTime: {MaxBulkCreationUtcDateTime}, " + $"MinBulkCreationUtcDateTime: {MinBulkCreationUtcDateTime}, " + $"Limit: {Limit}, " + $"IsUnmonitored: {IsUnmonitored}, " + $"IsAlerted: {IsAlerted}.";
public string NewToString()
return new StringBuilder().AppendPropertyInfo(this, x => x.MaxBulkCreationUtcDateTime).AppendPropertyInfo(this, x => x.MinBulkCreationUtcDateTime).AppendPropertyInfo(this, x => x.Limit).AppendPropertyInfo(this, x => x.IsUnmonitored).AppendPropertyInfo(this, x => x.IsAlerted).Append(".").ToString();
public static class StringBuilderExtensions
public static StringBuilder AppendPropertyInfo<TSource, TOutput>(this StringBuilder builder, TSource source, Expression<Func<TSource, TOutput>> propertyExpression)
if (!(propertyExpression.Body is MemberExpression memberExpression))
throw new ArgumentOutOfRangeException(nameof(propertyExpression), "Lambda be in format source.Property");
builder.AppendFormat("{0}: {1}", memberExpression.Member.Name, propertyExpression.Compile().Invoke(source));