If you just write to the console, that is, debug the output of styles, then what you want is the minimum chance of typo / copy errors.
This is confusing under the hood, but very effective on call sites:
public static void WriteNameAndValue<T,TValue>( this TextWriter tw, T t, Expression<Func<T, TValue>> getter) { var memberExpression = getter.Body as MemberExpression; if (memberExpression == null) throw new ArgumentException("missing body!"); var member = memberExpression.Member; tw.Write(member.Name); tw.Write(": "); if (member is FieldInfo) { tw.Write(((FieldInfo)member).GetValue(t)); } else if (member is PropertyInfo) { tw.Write(((PropertyInfo)member).GetValue(t, null)); } } public static void WriteNameAndValueLine<T,TValue>( this TextWriter tw, T t, Expression<Func<T, TValue>> getter) { WriteNameAndValue<T,TValue>(tw, t, getter); tw.WriteLine(); }
then you can write
t.Foo = "bar"; t.Bar = 32.5; Console.Out.WriteNameAndValueLine(t, x => x.Foo); Console.Out.WriteNameAndValueLine(t, x => x.Bar);
If you want this to be more customizable at runtime through resources and with considerations for localization, you can do this, but I would consider a different, more standardized approach if that was a likely requirement.
PS if you want a fantasy, you can replace the FieldInfo / PropertyInfo switch with
tw.Write(getter.Compile()(t));
and then you can also check MethodInfo in the expression (or allow arbitrary lambdas and just insert a line number or some other common text. I suggest not letting this route go down, use is already confusing and this can lead to unwanted downloads that should be simple logging method.