I'm hoping there's an expert in using the FileHelpers library here.
I'm using the 2.9.9 stable version from Nuget and trying to use the AfterReadRecord event handler to test if fields are empty.
The code I have is shown in simplified form below:
public class Test
{
public class MyClass
{
public string Name;
}
public static void engine_AfterReadRecord(EngineBase engine, FileHelpers.Events.AfterReadEventArgs<MyClass> e)
{
if (String.IsNullOrWhiteSpace(e.Record.Name))
{
throw new InvalidDataException("Name is required");
}
}
public void ReadCSV()
{
FileHelperEngine engine = new FileHelperEngine(typeof(MyClass));
engine.Options.IgnoreFirstLines = 1;
engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue;
engine.AfterReadRecord += new FileHelpers.Events.AfterReadHandler<MyClass>(engine_AfterReadRecord);
}
}
There's a compilation error on that last line in the ReadCSV file. The error is:
Cannot implicitly convert type 'FileHelpers.Events.AfterReadHandler<MyClass>' to
'FileHelpers.Events.AfterReadHandler<object>'
I'm stuck. Any helpers?
Jake
The problem is that u are using a version of the engines that don't use generics but using generics in the handler
Try with this:
var engine = new FileHelperEngine<MyClass>();
engine.Options.IgnoreFirstLines = 1;
engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue;
engine.AfterReadRecord += new FileHelpers.Events.AfterReadHandler<MyClass>(engine_AfterReadRecord);