There is the IFormattable interface...
class MyObject : IFormattable
{
public string ToString(string format, IFormatProvider formatProvider)
{
throw new NotImplementedException();
}
}
Probably you only have to implement it.
The IFormattable interface converts an object to its string representation based on a format string and a format provider.
Try with
class Foo : IFormattable
{
public string ToString(string format, IFormatProvider formatProvider)
{
return string.Format("Foo formatted as {0}", format != null ? format : "(null)");
}
}
Console.WriteLine("{0}, {0:xxx}", new Foo());
The {0}
is passed as null
, while the {0:xxx}
is passed as xxx