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.:

  1. If formatProvider is null, should I fall back to this.ToString()?
  2. If formatProvider.GetFormat(typeof(ICustomFormatter)) returns null, is there a particular exception I should throw?

Any blog posts / code samples / MSDN references are appreciated.

Edit
Report