简体   繁体   English

从写入文本字段Filehelpers中删除可选字段

[英]Removing the optional fields from writing to text field Filehelpers

How do I go about removing the optional field from the text field that I have output using the Filehelpers library. 如何从使用Filehelpers库输出的文本字段中删除可选字段。 I'm using c# 我正在使用c#

For example, I have a shared class file with attributes such as recordnumber, filler, payment, endingspaces 例如,我有一个共享的类文件,其中包含recordnumber,filler,payment,endingspaces等属性

Then I need to write only recordnumber and payment into the text file without the filler. 然后我需要只在没有填充符的情况下将recordnumber和payment写入文本文件。

[FixedLengthRecord(FixedMode.ExactLength)]
public partial class Person
{
[FieldFixedLength(10)]
public string FirstName;
[FieldFixedLength(10)]
public string LastName;

[FieldOptional]
[FieldFixedLength(5)]
public string Optional1;

[FieldOptional]
[FieldFixedLength(5)]
public string Optional2;

[FieldOptional]
[FieldFixedLength(5)]
public string Optional3;

} }

class Program
{
private static void Main(string[] args)
 {
  var engine = new FileHelperEngine<Person>();
  Person[] allPersonRecords = GetPersonExportFromDataBase() as Person[];//This will only get the FirstName,LastName,Optional2. No result for Optional1 and Optional3

  FileHelperEngine enginePerson = new FileHelperEngine(typeof(Person));

  enginePerson.AppendToFile(FileName, allPersonRecords ); //Write the records to the file

//Current Output looks like this:John      Lee            title     
//The output which I want is:John      Lee       title

 }
}

You can use the INotifyWrite attribute to intercept the line before it is written and modify it. 您可以使用INotifyWrite属性在写入行之前拦截该行并对其进行修改。 Here is a working example. 这是一个有效的例子。

[DelimitedRecord(",")]
public partial class Person : INotifyWrite<Person>
{
    public string FirstName;
    public string LastName;
    [FieldOptional]
    public string Optional1;
    [FieldOptional]
    public string Optional2;
    [FieldOptional]
    public string Optional3;

    public void BeforeWrite(BeforeWriteEventArgs<Person> e)
    {
    }

    public void AfterWrite(AfterWriteEventArgs<Person> e)
    {
        // count the non-optional fields
        var numberOfNonOptionalFields = typeof(Person).GetFields()
            .Where(f => !f.GetCustomAttributes(false).Any(a => a is FieldOptionalAttribute))
            .Count();

        // take only the first n tokens
        e.RecordLine = String.Join(",", e.RecordLine.Split(',').Take(numberOfNonOptionalFields));
    }
}      

class Program
{
    private static void Main(string[] args)
    {
        var engine = new FileHelperEngine<Person>();
        var export = engine.WriteString(
                     new Person[] { 
                       new Person() { 
                           FirstName = "Joe", 
                           LastName = "Bloggs", 
                           Optional1 = "Option 1", 
                           Optional2 = "Option 2", 
                           Optional3 = "Option 3" 
                       } 
                     });
        Assert.AreEqual("Joe,Bloggs" + Environment.NewLine, export);
        Console.WriteLine("Export was as expected");
        Console.ReadLine();
    }
}

If you want to just statically omit the optional fields (ie they are never used and you never want to output them) you could just create another class representing the desired output format and then convert the list from one object type to another using LINQ: 如果你想静态地省略可选字段(即它们从未被使用过而你永远不想输出它们),你可以创建另一个表示所需输出格式的类,然后使用LINQ将列表从一种对象类型转换为另一种:

class Program
{
    static void Main(string[] args)
    {
        // dummy test data
        var originalAllPersonRecords = new Person[]
        {
            new Person { FirstName = "John", LastName = "Lee", Optional2 = "title" },
        };//This will only get the FirstName,LastName,Optional2. No result for Optional1 and Optional3

        var allPersonRecords = from p in originalAllPersonRecords select new OutputPerson{ FirstName = p.FirstName, LastName = p.LastName, Optional2 = p.Optional2 };

        FileHelperEngine enginePerson = new FileHelperEngine(typeof(OutputPerson));

        string fileName = "Wherever you want the file to go";
        enginePerson.AppendToFile(fileName, allPersonRecords); //Write the records to the file

        //Current Output looks like this:John      Lee            title     
        //The output which I want is:John      Lee       title
    }
}
//New class added representing the output format
[FixedLengthRecord(FixedMode.ExactLength)]
class OutputPerson
{
    [FieldFixedLength(10)]
    public string FirstName;

    [FieldFixedLength(10)]
    public string LastName;

    [FieldOptional]
    [FieldFixedLength(5)]
    public string Optional2;
}

[FixedLengthRecord(FixedMode.ExactLength)]
class Person
{
    [FieldFixedLength(10)]
    public string FirstName;
    [FieldFixedLength(10)]
    public string LastName;

    [FieldOptional]
    [FieldFixedLength(5)]
    public string Optional1;

    [FieldOptional]
    [FieldFixedLength(5)]
    public string Optional2;

    [FieldOptional]
    [FieldFixedLength(5)]
    public string Optional3;
}

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

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