Search code examples
c#csvhelper

CsvHelper update, deriving DefaultTypeConverter


Recently I've updated CsvHelper from v2 to v15 and the following code does not work anymore:

public class DateConverter : DefaultTypeConverter
    {
        public override string ConvertToString(TypeConverterOptions options, object value)
        {
            return ((DateTime)value).ToString("dd'/'MM'/'yyyy");
        }
    }

Error is: no suitable method found to override but DefaultTypeConverter from assembly is:

public class DefaultTypeConverter : ITypeConverter
{
    public DefaultTypeConverter();

    //
    // Summary:
    //     Converts the string to an object.
    //
    // Parameters:
    //   text:
    //     The string to convert to an object.
    //
    //   row:
    //     The CsvHelper.IReaderRow for the current record.
    //
    //   memberMapData:
    //     The CsvHelper.Configuration.MemberMapData for the member being created.
    //
    // Returns:
    //     The object created from the string.
    public virtual object ConvertFromString(string text, IReaderRow row, MemberMapData memberMapData);
    //
    // Summary:
    //     Converts the object to a string.
    //
    // Parameters:
    //   value:
    //     The object to convert to a string.
    //
    //   row:
    //     The CsvHelper.IWriterRow for the current record.
    //
    //   memberMapData:
    //     The CsvHelper.Configuration.MemberMapData for the member being written.
    //
    // Returns:
    //     The string representation of the object.
    public virtual string ConvertToString(object value, IWriterRow row, MemberMapData memberMapData);
}

How can I overcome this?


Solution

  • The signature of your override does not match the signature of the method in the class you are extending. This has changes between versions of csvhelper

    You'll need to replace your current implementation with this:

    public override string ConvertToString(object value, IWriterRow row, MemberMapData memberMapData)
    {
        return ((DateTime)value).ToString("dd'/'MM'/'yyyy");
    }
    

    For more changes you could look up CsV Helper on GitHub