From 14c0dc63c74194531018ea31b9267399b3c80825 Mon Sep 17 00:00:00 2001 From: Milan Figuric Date: Tue, 20 Oct 2015 19:34:00 +0200 Subject: [PATCH] Add Regex Pattern for CSV Column --- LINQtoCSV/CsvColumnAttribute.cs | 1 + LINQtoCSV/FieldMapper.cs | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/LINQtoCSV/CsvColumnAttribute.cs b/LINQtoCSV/CsvColumnAttribute.cs index a1c259d..297261f 100644 --- a/LINQtoCSV/CsvColumnAttribute.cs +++ b/LINQtoCSV/CsvColumnAttribute.cs @@ -21,6 +21,7 @@ public class CsvColumnAttribute : System.Attribute public NumberStyles NumberStyle { get; set; } public string OutputFormat { get; set; } public int CharLength { get; set; } + public string RegexPattern { get; set; } public CsvColumnAttribute() { diff --git a/LINQtoCSV/FieldMapper.cs b/LINQtoCSV/FieldMapper.cs index 9ef9dfc..a1be7f3 100644 --- a/LINQtoCSV/FieldMapper.cs +++ b/LINQtoCSV/FieldMapper.cs @@ -4,6 +4,7 @@ using System.ComponentModel; using System.Linq; using System.Reflection; +using System.Text.RegularExpressions; namespace LINQtoCSV { @@ -32,6 +33,7 @@ protected class TypeFieldInfo : IComparable public MethodInfo parseNumberMethod = null; public MethodInfo parseExactMethod; public int charLength = 0; + public string RegexPattern { get; set; } // ---- @@ -131,7 +133,7 @@ private TypeFieldInfo AnalyzeTypeField( { tfi.name = cca.Name; } - + tfi.RegexPattern = cca.RegexPattern; tfi.index = cca.FieldIndex; tfi.hasColumnAttribute = true; tfi.canBeNull = cca.CanBeNull; @@ -545,6 +547,12 @@ public T ReadObject(IDataRow row, AggregatedException ae) { { try { + if (tfi.RegexPattern != null && !Regex.IsMatch(value, tfi.RegexPattern)) + { + throw new WrongDataFormatException(typeof(T).ToString(), tfi.name, value, row[i].LineNbr, m_fileName, null); + } + + Object objValue = null; // Normally, either tfi.typeConverter is not null,