/// <summary> /// Create a list of fields we are extracting and set /// the size hint for record I/O /// </summary> private void InitRecordFields() { //Debug.Assert(false, "TODO: Add RecordFilter to the engine."); var recordAttribute = Attributes.GetFirstInherited <TypedRecordAttribute>(RecordType); if (recordAttribute == null) { throw new BadUsageException(Messages.Errors.ClassWithOutRecordAttribute .ClassName(RecordType.Name) .Text); } if (ReflectionHelper.GetDefaultConstructor(RecordType) == null) { throw new BadUsageException(Messages.Errors.ClassWithOutDefaultConstructor .ClassName(RecordType.Name) .Text); } Attributes.WorkWithFirst <IgnoreFirstAttribute>( RecordType, a => IgnoreFirst = a.NumberOfLines); Attributes.WorkWithFirst <IgnoreLastAttribute>( RecordType, a => IgnoreLast = a.NumberOfLines); Attributes.WorkWithFirst <IgnoreEmptyLinesAttribute>( RecordType, a => { IgnoreEmptyLines = true; IgnoreEmptySpaces = a.mIgnoreSpaces; }); Attributes.WorkWithFirst <IgnoreCommentedLinesAttribute>( RecordType, a => { IgnoreEmptyLines = true; CommentMarker = a.mCommentMarker; CommentAnyPlace = a.mAnyPlace; }); Attributes.WorkWithFirst <ConditionalRecordAttribute>( RecordType, a => { RecordCondition = a.Condition; RecordConditionSelector = a.ConditionSelector; if (RecordCondition == RecordCondition.ExcludeIfMatchRegex || RecordCondition == RecordCondition.IncludeIfMatchRegex) { RecordConditionRegEx = new Regex(RecordConditionSelector, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); } }); if (CheckGenericInterface(RecordType, typeof(INotifyRead <>), RecordType)) { NotifyRead = true; } if (CheckGenericInterface(RecordType, typeof(INotifyWrite <>), RecordType)) { NotifyWrite = true; } // Create fields // Search for cached fields var fields = new List <FieldInfo>(ReflectionHelper.RecursiveGetFields(RecordType)); Fields = CreateCoreFields(fields, recordAttribute); FieldCount = Fields.Length; if (FieldCount == 0) { throw new BadUsageException(Messages.Errors.ClassWithOutFields .ClassName(RecordType.Name) .Text); } if (recordAttribute is FixedLengthRecordAttribute) { // Defines the initial size of the StringBuilder SizeHint = 0; for (int i = 0; i < FieldCount; i++) { SizeHint += ((FixedLengthField)Fields[i]).FieldLength; } } }
/// <summary> /// Create a list of fields we are extracting and set /// the size hint for record I/O /// </summary> private void InitRecordFields() { var recordAttribute = Attributes.GetFirstInherited <TypedRecordAttribute>(RecordType); if (recordAttribute == null) { throw new BadUsageException($"The record class {RecordType.Name} must be marked with the [DelimitedRecord] or [FixedLengthRecord] Attribute"); } if (ReflectionHelper.GetDefaultConstructor(RecordType) == null) { throw new BadUsageException($"The record class {RecordType.Name} needs a constructor with no args (public or private)"); } Attributes.WorkWithFirst <IgnoreFirstAttribute>( RecordType, a => IgnoreFirst = a.NumberOfLines); Attributes.WorkWithFirst <IgnoreLastAttribute>( RecordType, a => IgnoreLast = a.NumberOfLines); Attributes.WorkWithFirst <IgnoreEmptyLinesAttribute>( RecordType, a => { IgnoreEmptyLines = true; IgnoreEmptySpaces = a.IgnoreSpaces; }); #pragma warning disable CS0618 // Type or member is obsolete Attributes.WorkWithFirst <IgnoreCommentedLinesAttribute>( #pragma warning restore CS0618 // Type or member is obsolete RecordType, a => { IgnoreEmptyLines = true; CommentMarker = a.CommentMarker; CommentAnyPlace = a.AnyPlace; }); Attributes.WorkWithFirst <ConditionalRecordAttribute>( RecordType, a => { RecordCondition = a.Condition; RecordConditionSelector = a.ConditionSelector; if (RecordCondition == RecordCondition.ExcludeIfMatchRegex || RecordCondition == RecordCondition.IncludeIfMatchRegex) { RecordConditionRegEx = new Regex(RecordConditionSelector, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); } }); if (CheckInterface(RecordType, typeof(INotifyRead))) { NotifyRead = true; } if (CheckInterface(RecordType, typeof(INotifyWrite))) { NotifyWrite = true; } // Create fields // Search for cached fields var fields = new List <FieldInfo>(ReflectionHelper.RecursiveGetFields(RecordType)); Fields = CreateCoreFields(fields, recordAttribute); if (FieldCount == 0) { throw new BadUsageException($"The record class {RecordType.Name} doesn't contain any fields"); } if (recordAttribute is FixedLengthRecordAttribute) { // Defines the initial size of the StringBuilder SizeHint = 0; for (int i = 0; i < FieldCount; i++) { SizeHint += ((FixedLengthField)Fields[i]).FieldLength; } } }