Search code examples
c#datetimefilehelpers

How to return DateTime.Today as a FieldNullValue in FileHelpers


I'm trying to set the FieldNullValue for a DateTime variable in a FileHelpers class to the Today value (so if when I read the .csv there is no entry, it defaults to Today's date). The code I've tried is:

[FieldOptional]
[FieldNullValue(DateTime.Today)]
public DateTime DATE;

However I get the error

"Error 1 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type".

Please can someone explain what I'm doing wrong?


Solution

  • The obvious approach would be

    [FieldOptional]
    [FieldNullValue(typeof(DateTime), DateTime.Today.ToString("M-dd-yyyy"))] 
    public DateTime DATE; 
    

    But it seems that FieldNullValueAttribute is unable to evaluate expressions like is the case for DateTime.Today.ToString() , so the only way to go for you is to hardcode a fixed date as string (not what we want in our code):

    [FieldOptional]
    [FieldNullValue(typeof(DateTime), "5/15/2015")] 
    public DateTime DATE;