public ChoKVPRecordFieldConfiguration this[string name]
 {
     get
     {
         return(KVPRecordFieldConfigurations.Where(i => i.Name == name).FirstOrDefault());
     }
 }
        public ChoKVPRecordConfiguration IgnoreField(string fieldName)
        {
            var fc = KVPRecordFieldConfigurations.Where(f => f.DeclaringMember == fieldName || f.FieldName == fieldName).FirstOrDefault();

            if (fc != null)
            {
                KVPRecordFieldConfigurations.Remove(fc);
            }

            return(this);
        }
        public ChoKVPRecordConfiguration IgnoreField <T, TProperty>(Expression <Func <T, TProperty> > field)
        {
            if (KVPRecordFieldConfigurations.Count == 0)
            {
                MapRecordFields <T>();
            }

            var fc = KVPRecordFieldConfigurations.Where(f => f.DeclaringMember == field.GetFullyQualifiedMemberName()).FirstOrDefault();

            if (fc != null)
            {
                KVPRecordFieldConfigurations.Remove(fc);
            }

            return(this);
        }
        public override void Validate(object state)
        {
            if (state == null)
            {
                base.Validate(state);

                if (Separator.IsNullOrWhiteSpace())
                {
                    throw new ChoRecordConfigurationException("Separator can't be null or whitespace.");
                }
                if (Separator == EOLDelimiter)
                {
                    throw new ChoRecordConfigurationException("Separator [{0}] can't be same as EODDelimiter [{1}]".FormatString(Separator, EOLDelimiter));
                }
                if (Separator.Contains(QuoteChar))
                {
                    throw new ChoRecordConfigurationException("QuoteChar [{0}] can't be one of Delimiter characters [{1}]".FormatString(QuoteChar, Separator));
                }
                if (Comments != null && Comments.Contains(Separator))
                {
                    throw new ChoRecordConfigurationException("One of the Comments contains Delimiter. Not allowed.");
                }
                if (RecordStart.IsNullOrWhiteSpace() && RecordEnd.IsNullOrWhiteSpace())
                {
                }
                else
                {
                    if (RecordStart.IsNullOrWhiteSpace())
                    {
                        throw new ChoRecordConfigurationException("RecordStart is missing.");
                    }
                    //else if (RecordEnd.IsNullOrWhiteSpace())
                    //    RecordEnd = RecordStart;
                    //throw new ChoRecordConfigurationException("RecordEnd is missing.");

                    if (RecordStart.Contains("*") || RecordStart.Contains("?"))
                    {
                        _isWildcardComparisionOnRecordStart = true;
                        _recordStartWildCard = new ChoWildcard(RecordStart);
                    }
                    if (!RecordEnd.IsNullOrWhiteSpace() && (RecordEnd.EndsWith("*") || RecordStart.Contains("?")))
                    {
                        _isWildcardComparisionOnRecordEnd = true;
                        _recordEndWildCard = new ChoWildcard(RecordEnd);
                    }
                }

                //Validate Header
                if (FileHeaderConfiguration != null)
                {
                    if (FileHeaderConfiguration.FillChar != null)
                    {
                        if (FileHeaderConfiguration.FillChar.Value == ChoCharEx.NUL)
                        {
                            throw new ChoRecordConfigurationException("Invalid '{0}' FillChar specified.".FormatString(FileHeaderConfiguration.FillChar));
                        }
                        if (Separator.Contains(FileHeaderConfiguration.FillChar.Value))
                        {
                            throw new ChoRecordConfigurationException("FillChar [{0}] can't be one of Delimiter characters [{1}]".FormatString(FileHeaderConfiguration.FillChar, Separator));
                        }
                        if (EOLDelimiter.Contains(FileHeaderConfiguration.FillChar.Value))
                        {
                            throw new ChoRecordConfigurationException("FillChar [{0}] can't be one of EOLDelimiter characters [{1}]".FormatString(FileHeaderConfiguration.FillChar.Value, EOLDelimiter));
                        }
                        if ((from comm in Comments
                             where comm.Contains(FileHeaderConfiguration.FillChar.Value.ToString())
                             select comm).Any())
                        {
                            throw new ChoRecordConfigurationException("One of the Comments contains FillChar. Not allowed.");
                        }
                    }
                }
            }
            else
            {
                string[] headers = state as string[];
                if (AutoDiscoverColumns &&
                    KVPRecordFieldConfigurations.Count == 0)
                {
                    AutoDiscoveredColumns = true;
                    if (headers != null && IsDynamicObject)
                    {
                        KVPRecordFieldConfigurations = (from header in headers
                                                        select new ChoKVPRecordFieldConfiguration(header)).ToList();
                    }
                    else
                    {
                        MapRecordFields(RecordType);
                    }
                }

                if (KVPRecordFieldConfigurations.Count <= 0)
                {
                    throw new ChoRecordConfigurationException("No record fields specified.");
                }

                //Validate each record field
                foreach (var fieldConfig in KVPRecordFieldConfigurations)
                {
                    fieldConfig.Validate(this);
                }

                //Check if any field has empty names
                if (KVPRecordFieldConfigurations.Where(i => i.FieldName.IsNullOrWhiteSpace()).Count() > 0)
                {
                    throw new ChoRecordConfigurationException("Some fields has empty field name specified.");
                }

                //Check field names for duplicate
                string[] dupFields = KVPRecordFieldConfigurations.GroupBy(i => i.FieldName, FileHeaderConfiguration.StringComparer)
                                     .Where(g => g.Count() > 1)
                                     .Select(g => g.Key).ToArray();

                if (dupFields.Length > 0 /* && !IgnoreDuplicateFields */)
                {
                    throw new ChoRecordConfigurationException("Duplicate field name(s) [Name: {0}] found.".FormatString(String.Join(",", dupFields)));
                }

                PIDict = new Dictionary <string, System.Reflection.PropertyInfo>();
                PDDict = new Dictionary <string, PropertyDescriptor>();
                foreach (var fc in KVPRecordFieldConfigurations)
                {
                    if (fc.PropertyDescriptor == null)
                    {
                        fc.PropertyDescriptor = ChoTypeDescriptor.GetProperties(RecordType).Where(pd => pd.Name == fc.Name).FirstOrDefault();
                    }
                    if (fc.PropertyDescriptor == null)
                    {
                        continue;
                    }

                    PIDict.Add(fc.PropertyDescriptor.Name, fc.PropertyDescriptor.ComponentType.GetProperty(fc.PropertyDescriptor.Name));
                    PDDict.Add(fc.PropertyDescriptor.Name, fc.PropertyDescriptor);
                }


                RecordFieldConfigurationsDict  = KVPRecordFieldConfigurations.Where(i => !i.Name.IsNullOrWhiteSpace()).GroupBy(i => i.Name).Select(g => g.First()).ToDictionary(i => i.Name, FileHeaderConfiguration.StringComparer);
                RecordFieldConfigurationsDict2 = KVPRecordFieldConfigurations.Where(i => !i.FieldName.IsNullOrWhiteSpace()).GroupBy(i => i.Name).Select(g => g.First()).ToDictionary(i => i.FieldName, FileHeaderConfiguration.StringComparer);
                AlternativeKeys = RecordFieldConfigurationsDict2.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Name, FileHeaderConfiguration.StringComparer);
                FCArray         = RecordFieldConfigurationsDict.ToArray();

                LoadNCacheMembers(KVPRecordFieldConfigurations);
            }
        }