0

我正在使用 C# FileHelpers 库来有效地将大型分隔文件解析为验证对象。

但是,我希望能够将单个输入文件列映射到多个输出类属性,但找不到明显的方法来实现这一点。我查看了 ITransformable,但我不想映射到另一个对象以在操作期间减少内存,我查看了 DynamicFieldBuilder/DynamicClassBuilder 对象,但这些似乎只允许我描述什么是在输入文件中,而不是输出实例中的内容。

我试图避免必须加载文件两次或事后进行某种对象到对象的映射。

输入文件示例:

ColumnA|ColumnB
Foo|Baz

输出类示例:

public class FooBar
{
    public string ColumnA_One;
    public string ColumnA_Two;
    public string ColumnB_One;
    public string ColumnB_Two;
}
4

2 回答 2

1

FieldIgnored您可以使用属性标记重复的列并使用AfterReadRecord事件来填充它们。

class Program
{
    [DelimitedRecord("|")]
    public class FooBar
    {
        public string ColumnA_One;       
        [FieldIgnored]
        public string ColumnA_Two;        

        public string ColumnB_One;        
        [FieldIgnored]
        public string ColumnB_Two;
    }

    static void Main(string[] args)
    {
        FileHelperEngine engine = new FileHelperEngine(typeof(FooBar));
        engine.AfterReadRecord += engine_AfterReadRecord;
        FooBar[] records = engine.ReadFile("FileIn.txt") as FooBar[];
    }

    static void engine_AfterReadRecord(EngineBase engine, FileHelpers.Events.AfterReadEventArgs<object> e)
    {
        FooBar fooBar = e.Record as FooBar;
        fooBar.ColumnA_Two = fooBar.ColumnA_One;
        fooBar.ColumnB_Two = fooBar.ColumnB_One;
    }
}
于 2012-01-12T11:50:33.823 回答
0
enum DataFields
    {
        Action = 0,
        CRC = 1,
        Desc = 2,
        Group = 3,
        Name = 4,
        Sell = 5,
        EffDate = 6,
        Whse = 7,
        Whse2 = 8,
        Whse3 = 9,
        Casepack = 10,
        LDU = 11,
        Cube = 12,
        Item = 13,
        UPC = 14,
        Cost = 15,
        Markup = 16,
        OldSell = 17,
        Avail = 18,
        Substitute = 19,
        Poll = 20
    }//enum DataFields

    /// <summary>
    /// This will hold the ordinal position of the NameFields within the datafile
    /// </summary>
    enum NameFields
    {
        Code = 0,
        Abrv = 1,
        Name = 2,
        Count = 3
    }//enum NameFields

    /// <summary>
    /// This will hold the ordinal position of the values when populating the history table
    /// </summary>
    enum HistoryFields
    {
        CRC = 0,
        EffDate = 1,
        OldSell = 2,
        Sell = 3
    }//enum HistoryFields
    #endregion    
于 2012-01-03T22:28:27.843 回答