in Education by
I think the direct answer to the question is 'No' but I'm hoping that someone has written a real simple library to do this (or I can do it...ugh...) Let me demonstrate what I am looking for with an example. Suppose I had the following: class Person { string Name {get; set;} int NumberOfCats {get; set;} DateTime TimeTheyWillDie {get; set;} } I would like to be able to do something like this: static void Main() { var p1 = new Person() {Name="John", NumberOfCats=0, TimeTheyWillDie=DateTime.Today}; var p2 = new Person() {Name="Mary", NumberOfCats=50, TimeTheyWIllDie=DateTime.Max}; var str = String.Format( "{0:Name} has {0:NumberOfCats} cats and {1:Name} has {1:NumberOfCats} cats. They will die {0:TimeTheyWillDie} and {1:TimeTheyWillDie} respectively ", p1, p2); Console.WriteLine(str); } Does anyone know if theres a format for doing something like this or if someone has written a library to do it? I know it shouldn't be too hard, but I'd rather not be reimplementing the wheel. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Edit: You don't have to implement IFormattable for each object...that'd be a PITA, severely limiting, and a fairly large maintenance burden. Just use Reflection and a IFormatProvider with ICustomFormatter and it'll work with any object. String.Format has an overload to take one as a parameter. I've never thought of this before, but you intrigued me - so I had to give it a quick whirl. Note that I chose to allow an additional format string to be passed to the property value, and that it only works with non indexed and accessible properties (though you could easily add that). public class ReflectionFormatProvider : IFormatProvider, ICustomFormatter { public object GetFormat(Type formatType) { return formatType == typeof(ICustomFormatter) ? this : null; } public string Format(string format, object arg, IFormatProvider formatProvider) { string[] formats = (format ?? string.Empty).Split(new char[] { ':' }, 2); string propertyName = formats[0].TrimEnd('}'); string suffix = formats[0].Substring(propertyName.Length); string propertyFormat = formats.Length > 1 ? formats[1] : null; PropertyInfo pi = arg.GetType().GetProperty(propertyName); if (pi == null || pi.GetGetMethod() == null) { // Pass thru return (arg is IFormattable) ? ((IFormattable)arg).ToString(format, formatProvider) : arg.ToString(); } object value = pi.GetGetMethod().Invoke(arg, null); return (propertyFormat == null) ? (value ?? string.Empty).ToString() + suffix : string.Format("{0:" + propertyFormat + "}", value); } } And your slightly modified example: var p1 = new Person() {Name="John", NumberOfCats=0, TimeTheyWillDie=DateTime.Today}; var p2 = new Person() {Name="Mary", NumberOfCats=50, TimeTheyWillDie=DateTime.MaxValue}; var str = string.Format( new ReflectionFormatProvider(), @"{0:Name} has {0:NumberOfCats} cats and {1:Name} has {1:NumberOfCats} cats. They will die {0:TimeTheyWillDie:MM/dd/yyyy} and {1:TimeTheyWillDie} respectively. This is a currency: {2:c2}.", p1, p2, 8.50M ); Console.WriteLine(str); Outputs: John has 0 cats and Mary has 50 cats. They will die 12/10/2008 and 12/31/9999 11:59:59 PM respectively. This is a currency: $8.50.

Related questions

0 votes
    I am trying to convert a string into date format in Google Sheet. I've tried different formulas but ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    Which of the following is a method of wrapper Float for converting the value of an object into byte? ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Which of the following is method of wrapper Integer for converting the value of an object into int? (a ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    I am trying to input text into the text box in 'www.google.com' and though it says the text ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    Registering members and allowing them to login (updating tables etc) all worked fine up until I made this change recently. ... , make the UserLevel 'Member'? Code for signing up:...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    How to convert JSON string into an object?...
asked Jan 11, 2021 in Technology by JackTerrance
0 votes
    Consider the impedance function, Z(s)=((s+4)(s+8))/((s+2)(s+6)). Find the value of R1 after ... theory Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    Consider the impedance function, Z(s)=((s+4)(s+8))/((s+2)(s+6)). Find the value of L2 after ... theory Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    Consider the impedance function, Z(s)=((s+4)(s+8))/((s+2)(s+6)). Find the value of R2 after ... theory Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    Consider the impedance function, Z(s)=((s+4)(s+8))/((s+2)(s+6)). Find the value of L3 after ... theory Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    Consider the impedance function, Z(s)=((s+4)(s+8))/((s+2)(s+6)). Find the value of R3 after ... theory Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    Consider the impedance function, Z(s)=(2s^2+8s+6)/(s^2+8s+12). Find the value of R1 after converting ... Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    Consider the impedance function, Z(s)=(2s^2+8s+6)/(s^2+8s+12). Find the value of L1 after converting ... Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    Consider the impedance function, Z(s)=(2s^2+8s+6)/(s^2+8s+12). Find the value of R2 after converting ... Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
0 votes
    Consider the impedance function, Z(s)=(2s^2+8s+6)/(s^2+8s+12). Find the value of L2 after converting ... Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 12, 2021 in Education by JackTerrance
...