13
Is there a good reference implementation for IFormattable? I plan to have at least one custom IFormatProvider for my object, and I want to make sure that the wiring is correct for the different possible parameter sets passed to IFormattable.ToString(string, IFormatProvider).
What I have so far:
public class MyDataClass : IFormattable
{
/// <seealso cref="IFormattable.ToString(string, IFormatProvider)"/>
public string ToString(string format, IFormatProvider formatProvider)
{
ICustomFormatter formatter = (ICustomFormatter)formatProvider.GetFormat(typeof(ICustomFormatter));
return formatter.Format(format, this, formatProvider);
}
}
But it seems like there are other potential situations that should be covered, i.e.:
- If
formatProvideris null, should I fall back tothis.ToString()? - If
formatProvider.GetFormat(typeof(ICustomFormatter))returnsnull, is there a particular exception I should throw?
Any blog posts / code samples / MSDN references are appreciated.