0

I have a c# class called "SepaRecord", already defined in a dll which I cannot modify. I have a List at runtime which I need to convert into a fixed length file.

The approach which I have tried is, using the FixedLengthClassBuilder I create a runtime definition of the record, as below:

FixedLengthClassBuilder fileRecordBuilder = new FixedLengthClassBuilder("SepaRecord");
fileRecordBuilder.AddField("RecordCode", 2, typeof(string));
fileRecordBuilder.AddField("CurrencyCode", 3, typeof(string));
fileRecordBuilder.AddField("CreditAmount", 15, typeof(int));
// other fields follow....

RecordCode, CurrencyCode, CreditAmount, etc are fields defined the "SepaRecord" class in the DLL.

I would expect that if I pass the list to the FileHelperEngine, I would get back a delimited string:

List<SepaRecord> lst = this.goGetMyList();
string ret = this.FileHelperEngine.WriteString(lst);

But of course it did not work since the FixedLengthClassBuilder builts and compiles a type/class at runtime and compares it with the class/type of the argument.

Question is: Is there a better approach to do what I am trying to do?
And a suggestion: Why bother with type checks? Just compare on the field level. I think this would make the runtime engine more flexible.

Appreciate any help Chris

shamp00
  • 11,106
  • 4
  • 38
  • 81
chrisl08
  • 1,658
  • 1
  • 15
  • 24

1 Answers1

0

FileHelpers does not provide auto-mapping of fields from other classes. That functionality is handled better by other libraries like AutoMapper.

FileHelpers is a library for describing text files. The FixedLengthClassBuiler is creating a specification of the text output.

You need to map the contents of your List<SepaRecord> to an IEnumerable<T> where T is the type created by `

Type recordClass = fileRecordBuilder.CreateRecordClass();
var recordClassLst = lst.Select(sepaRecord => ConvertToRecordClass(sepaRecord));    
string ret = this.FileHelperEngine.WriteString(recordClassLst);

You'd need to code the ConvertToRecordClass function yourself, via AutoMapper if you like.

Additional note: Rather than using the FixedLengthClassBuilder you will probably find it easier to create a concrete class for your text file spec like this:

[FixedLengthRecord(",")]
public class MyClass
{
    [FixedLengthRecord(2)]
    public string RecordCode;
    [FixedLengthRecord(3)]
    public string CurrencyCode;
    [FixedLengthRecord(15)]
    public int CreditAmount;
    // etc.
}

Then it is easier to create an IEnumerable<MyClass>. (The FixedLengthClassBuiler is more useful when your class record definition is not known until run time).

shamp00
  • 11,106
  • 4
  • 38
  • 81