示例#1
0
        /// <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(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.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(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;
                }
            }
        }