I'm trying to figure out how IFormatProvider and ICustomFormatter work after following Format TimeSpan in DataGridView column on how to customize a TimeSpan in a DataGridView. I've created a completely custom formatter that always returns "foo" regardless of what it is formatting.
I'm using it on Int but I assume it should work on all types as it doesn't check the value being passed, it just returns "foo"
.
class MyFormatter : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
Console.WriteLine("GetFormat");
return this;
}
public string Format(string format, object arg, IFormatProvider formatProvider)
{
Console.WriteLine("Format");
return "foo";
}
}
And I'm passing it to int.ToString()
:
int number = 10;
Console.WriteLine(number.ToString(new MyFormatter()));
What I'm getting is:
GetFormat 10
While what I was hoping to get is:
GetFormat Format foo
Edit: I found How to create and use a custom IFormatProvider for DateTime? and the answers there say that DateTime.ToString()
will not accept anything but DateTimeFormatInfo
or CultureInfo
and an object will be rejected if it's not of these types even if it implements ICustomFormatter
- https://stackoverflow.com/a/2382481/492336.
So my question is then does that hold in all cases of the ToString()
methods? Does it hold also for DataGridView, and in which cases can I pass a truly custom formatter?