2016-09-08 2 views
0

FileHelpers를 사용하여 파일을 쓸 때 속성을 표시하지 않을 수 있습니까? FileHelpers를 사용하여 파일을 쓸 때 속성을 표시하지 않을 수 있습니까?

내가 객체가 말 :

[DelimitedRecord(",")] 
public class MyClass 
{ 
    public int Field1 { get; set; } 
    public string Field2 { get; set; } 
    public string Field3 { get; set; } 
    public string Field4 { get; set; } 
    public string Field5 { get; set; } 
} 

가 나는 CSV를 기록 할을하지만 (이 채워 또는 아니에요 관계없이 경우) 입력란 3을 생략합니다.

Ex.
출력은 다음과 같습니다. Field1, Field2, Field4, Field5
FileHelpers에서 파일 쓰기를 억제하는 데 사용할 수있는 특성이 있습니까?

답변

0

설명서 herehere에서 FieldValueDiscarded 특성을 사용합니다.

사용하지 않는 필드에는 FieldValueDiscarded 속성을 사용하십시오. 귀하의 기록 클래스는 사용하지 않는 일부 필드가있는 경우

, 도서관이 해결 방법으로이 속성

+0

이는 속성이 아닌 입력란에서만 작동하는 것으로 보입니다. 속성을 필드로 변환 할 때 오류가 발생하기 때문에 그 중 일부를 가지고 놀아야 할 것입니다. 내가 사용하고있는 클래스에는 70 개 이상의 속성이 있습니다. – PrivateJoker

0

로 표시 필드의 값을 무시합니다, 당신은 마지막으로 구분 기호를 제거 할 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(); 
    } 
} 
관련 문제