コード例 #1
0
ファイル: Scribe.cs プロジェクト: elliotschroeder/Scribe
        public void Write <T>(T record)
        {
            var file = string.Empty;

            PropertyInfo[]           properties    = record.GetType().GetProperties();
            Dictionary <int, string> fieldsToWrite = new Dictionary <int, string>();

            foreach (var prop in properties)
            {
                var attr = prop.GetCustomAttribute <FixedLengthField>();

                if (attr == null)
                {
                    continue;
                }

                var fieldValue = prop.GetValue(record) == null ? string.Empty : prop.GetValue(record).ToString();

                fieldsToWrite.Add(attr.FieldOrder, _fieldFormatter.WriteField(fieldValue, attr.FieldLength, attr.Alignment, attr.Padding, attr.CustomPaddingValue));
            }

            foreach (var field in fieldsToWrite.OrderBy(d => d.Key))
            {
                file += field.Value;
            }

            var fixedFileAttr = record.GetType().GetCustomAttribute <FixedLengthFile>();

            if (fixedFileAttr != null)
            {
                file = _fieldFormatter.FormatLineEndings(file, fixedFileAttr.LineLength);
            }

            _textWriter.Write(file);
        }
コード例 #2
0
        public void WriteField_Alignment_No_Padding_Defaults_To_Spaces(string value, string expected, Alignment alignment, Padding padding, int fieldLength, char paddingChar)
        {
            var result = _testObject.WriteField(value, fieldLength, alignment, padding, paddingChar);

            Assert.Equal(expected, result);
        }