Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Given the current state of FileHelpers, you really have to define that field as a string and try to do your own validation on it.</p> <p>I looked at a couple different ways of addressing this -- first, I tried setting up a record using a property rather than a field. This could conceivably have allowed you to create a String property and validate inputs on the Set. This turned out to be difficult because:</p> <ul> <li>Attributes are set to target fields only (fairly easy fix if you want to modify FileHelpers code.</li> <li>Private fields for properties have to be marked w/ [FieldNotInFile] attribute (annoyance).</li> <li>Properties aren't handled in RecordInfo.CreateCoreFields() method. This one is a little more involved to fix, since you really would have to create a merged list of properties and fields in the correct order. This is the point where I stopped looking into this method.</li> </ul> <p>Next plan: use the record definition as-is, and validate during the read. Set up an event handler on the engine:</p> <pre><code> engine.AfterReadRecord += new Events.AfterReadHandler&lt;CustomersFixedWithNumericId&gt;(engine_AfterReadRecord); var res = engine.ReadFile(path); </code></pre> <p>Then handle bad values in the handler:</p> <pre><code> void engine_AfterReadRecord(EngineBase engine, Events.AfterReadEventArgs&lt;CustomersFixedWithNumericId&gt; e) { int intVal; int.TryParse(e.Record.CustomerID, out intVal); e.Record.CustomerID = intVal.ToString(); } </code></pre> <p>Neither of these are perfect, but I think the second is reasonably close.</p> <p>Addendum: This shows the technique above with a late-bound class:</p> <pre><code>public void TestMethod1() { var assembly = System.Reflection.Assembly.GetExecutingAssembly(); Type t = assembly.GetType("FileHelpers.Tests.CustomersFixedWithNumericId"); FileHelperEngine engine = new FileHelperEngine(t); string path = @"pathtofile\BadCustomersFixedNumericId.txt"; engine.AfterReadRecord += new Events.AfterReadHandler&lt;object&gt;(engine_AfterReadRecord); var res = engine.ReadFile(path); } void engine_AfterReadRecord(EngineBase engine, Events.AfterReadEventArgs&lt;object&gt; e) { // validation here } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload