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

时间:2015-03-03 11:19:01

标签: c# optional filehelpers

如何从使用Filehelpers库输出的文本字段中删除可选字段。我正在使用c#

例如, 我有一个共享的类文件 具有记录编号,填充,付款,结束空间等属性

然后我只需要在没有填充符的情况下将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

 }
}

2 个答案:

答案 0 :(得分:0)

您可以使用INotifyWrite属性在编写行之前拦截该行并对其进行修改。这是一个有效的例子。

[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();
    }
}

答案 1 :(得分:0)

如果你想静态地省略可选字段(即它们永远不会使用而你从不想要输出它们)你可以创建另一个代表所需字段的类输出格式,然后使用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;
}