简体   繁体   English

FileHelpers:混合分隔和固定长度记录

[英]FileHelpers: mixing Delimited and Fixed Length Records

Here I have to write out a file which records are Pipe Separated, using FileHelpers and C#. 在这里,我必须使用FileHelpers和C#写出一个记录为Pipe Separated的文件。 Great part of fields have variable length (so, my records would be [DelimitedRecord("|")] ). 大部分字段都有可变长度(因此,我的记录将是[DelimitedRecord(“|”)])。 But some fields must have fixed length (they must have paddings, specific format and so on). 但是某些字段必须具有固定长度(它们必须具有填充,特定格式等)。

I've googled a bunch with no goal on how to accomplish that. 我已经搜索了一堆没有关于如何实现这一目标的目标。

Example: 例:

[DelimitedRecord("|")]
public class Customer
{
    public int CustId; //variable length

    public string Name; //variable length

    public decimal Balance; //variable length

    [FieldConverter(ConverterKind.Date, "dd-MM-yyyy")]
    public DateTime AddedDate;

    public int Code; // this one must have 10 characters with "zero-fill", like
             // 153 must look like 0000000153

}

How do I accomplish that? 我该如何做到这一点? Do I have to use a Converter approach and write my own converter for this? 我是否必须使用转换器方法并为此编写自己的转换器?

Thank you in advance. 先感谢您。

For anyone who comes across this question in the future, here is some working code to solve this problem. 对于将来遇到此问题的任何人,这里有一些工作代码来解决这个问题。

This class is a converter which the FileHelper engine will use to convert the integer to a string, padded with 0s up to the size specified in the constructor. 此类是一个转换器,FileHelper引擎将使用该转换器将整数转换为字符串,填充为0,最大为构造函数中指定的大小。

public class PaddedIntConverter:ConverterBase
{
    private int _size;
    public PaddedIntConverter(int size)
    {
        _size = size;
    }

    public override object StringToField(string from)
    {
        return int.Parse(from);
    }

    public override string FieldToString(object from)
    {
        return from.ToString().PadLeft(_size,'0');
    }
}

The converter can then be applied to your class like this: 然后转换器可以应用于您的类,如下所示:

[FixedLengthRecord(FixedMode.ExactLength)]
public class MyClass{
    [FieldFixedLength(7)]
    [FieldConverter(typeof(PaddedIntConverter), 7)]
    public int RecordCount;
}

FileHelpers有一个属性[FieldFixedLength(xxx)],我相信这应该能得到你想要的东西( http://filehelpers.sourceforge.net/attributes.html )。

As mentioned by @TYY, I wrote my own "multiuse" converter, just like this: 正如@TYY所提到的,我编写了自己的“多用途”转换器,就像这样:



    public StringNumberCharConverter(
        string Size, 
        string PaddingChar, 
        string PaddingType, 
        string RemoveSpecialChars)
    { 
        //implementation here 
    }

Since FileHelpers converters accept string args only, I had to parse everything on proper objects inside the Converter constructor. 由于FileHelpers转换器只接受字符串args,因此我必须解析Converter构造函数中正确对象的所有内容。

For the parameters, I've converted "Size" to an "integer", PaddingChar onto a "char", PaddingType onto a custom padding type enum (ie: Padding.LEFT or Padding.RIGHT, so if a "left" is comming from parameters, I should use String.PadLeft() and so on), and the "RemoveSpecialChars" parameter were converted onto a boolean (flag to check if the converter should remove special characters or not.) 对于参数,我将“Size”转换为“整数”,将PaddingChar转换为“char”,将PaddingType转换为自定义填充类型枚举(即:Padding.LEFT或Padding.RIGHT,如果“left”即将来临从参数,我应该使用String.PadLeft()等,并将“RemoveSpecialChars”参数转换为布尔值(标志,以检查转换器是否应删除特殊字符。)

Since I need Object-to-File conversion, all the conversion logic is inside "FieldToString" method implementation of ConverterBase abstract method. 由于我需要Object-to-File转换,所有转换逻辑都在ConverterBase抽象方法的“FieldToString”方法实现中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM