Skip to content

Custom Formatters

Michael Scribellito edited this page Jul 20, 2018 · 6 revisions

ITextFormatter Interface

To create a custom formatter, begin by implementing the ITextFormatter interface.

  • Deserialize deserialize a string using custom rules.
  • Serialize serialize an object using custom rules.
public class DateFormatter : ITextFormatter
{

    private const string Format = "yyyyMMdd";

    public object Deserialize(string value)
    {
        return DateTime.ParseExact(value, Format, null);
    }

    public string Serialize(object value)
    {
        return ((DateTime)value).ToString(Format);
    }

}

Set FormatterType to typeof() custom formatter class name.

[TextSerializable]
public class Dog
{
    ...
    [TextField(15, 8,
        FormatterType = typeof(DateFormatter))]
    public DateTime BirthDate { get; set; }
    ...
}

Next >> Frequently Asked Questions

Clone this wiki locally