FileHelpers的字段数不同

时间:2016-09-13 12:08:02

标签: vb.net filehelpers

我的程序必须以三种方式创建文件,如下所示

1 case: all fields and create file 
2 case: without Variation2 and Merkmalname2
3 case: without Variation2 and Merkmalname2 and Variation3 and Merkmalname3

班级代码:

Imports FileHelpers

<DelimitedRecord(",")>
Public Class CSV
    <FieldQuoted(QuoteMode.AlwaysQuoted)>
    Public Artikelnummer As String

    <FieldQuoted(QuoteMode.AlwaysQuoted)>
    Public Variation1 As String

    <FieldQuoted(QuoteMode.AlwaysQuoted), FieldOptional()>
    Public Variation2 As String

    <FieldQuoted(QuoteMode.AlwaysQuoted), FieldOptional()>
    Public Variation3 As String

    <FieldQuoted(QuoteMode.AlwaysQuoted)>
    Public Barcode As String

    <FieldQuoted(QuoteMode.AlwaysQuoted)>
    Public Merkmalname1 As String

    <FieldQuoted(QuoteMode.AlwaysQuoted), FieldOptional()>
    Public Merkmalname2 As String

    <FieldQuoted(QuoteMode.AlwaysQuoted), FieldOptional()>
    Public Merkmalname3 As String

End Class

因此我将这些字段标记为可选字段。但是我收到了错误。

第一案:

Dim WithAll = New CSV() With {
             .Artikelnummer = "my artikelname 2",
           .Variation1 = "my variation 2",
.Variation2 = "my variation 2",
.Variation3 = "my variation 2",
           .Barcode = "barcode 2",
           .Merkmalname1 = "blabla 2",
.Merkmalname2 = "blabla 2",
.Merkmalname3 = "blabla 2"}

        engine.HeaderText = engine.GetFileHeader
        engine.WriteFile("Output.txt", WithAll)

第二案:

Dim WithTwo = New CSV() With {
             .Artikelnummer = "my artikelname 2",
           .Variation1 = "my variation 2",
.Variation2 = "my variation 2",
           .Barcode = "barcode 2",
           .Merkmalname1 = "blabla 2",
.Merkmalname2 = "blabla 2"}

        engine.HeaderText = engine.GetFileHeader
        engine.WriteFile("Output.txt", WithTwo )

第3例:

Dim WithOne = New CSV() With {
             .Artikelnummer = "my artikelname 2",
           .Variation1 = "my variation 2",
           .Barcode = "barcode 2",
           .Merkmalname1 = "blabla 2"}

        engine.HeaderText = engine.GetFileHeader
        engine.WriteFile("Output.txt", WithOne )

我得到的错误适用于所有情况:

An unhandled exception of type 'FileHelpers.BadUsageException' occurred in FileHelpers.dll

Additional information: The field: VariationswertX must be marked as optional because the previous field is marked as optional. (Try adding [FieldOptional] to VariationswertX)

1 个答案:

答案 0 :(得分:0)

FieldOptional属性仅适用于导入。导出时将包含该字段。

作为一种变通方法,您可以使用AfterWrite事件删除最后一个分隔符。像这样:

[DelimitedRecord(",")]
class Product : INotifyWrite
{
    [FieldQuoted(QuoteMode.AlwaysQuoted)]
    public string Name;
    [FieldQuoted(QuoteMode.AlwaysQuoted)]
    public string Description;
    [FieldOptional]
    public string Size;

    public void BeforeWrite(BeforeWriteEventArgs e)
    {
        // prevent output of [FieldOptional] Size field
        Size = null;
    }

    public void AfterWrite(AfterWriteEventArgs e)
    {
        // remove last "delimiter"
        e.RecordLine = e.RecordLine.Remove(e.RecordLine.Length - 1, 1);
    }
}

class Program
{
    static void Main(string[] args)
    {
        var engine = new FileHelperEngine<Product>();
        var products = new Product[] { new Product() { Name = "Product", Description = "Details", Size = "Large"} };
        var productRecords = engine.WriteString(products);
        try
        {
            // Make sure Size field is not part of the output
            Assert.AreEqual(@"""Product"",""Details""" + Environment.NewLine, productRecords);
            Console.WriteLine("All tests pass");
        }
        catch (Exception ex)
        {
            Console.WriteLine("An error occurred");
            Console.WriteLine(ex);
        }

        Console.ReadKey();
    }
}