Skip to content

Instantly share code, notes, and snippets.

@vcaraulean
Created May 9, 2011 14:43
Show Gist options
  • Save vcaraulean/962645 to your computer and use it in GitHub Desktop.
Save vcaraulean/962645 to your computer and use it in GitHub Desktop.
Test case for BadUsageException
// Code is throwing exception:
// FileHelpers.BadUsageException : This engine works with record of type Country and you use records of type Country
// at FileHelpers.FileHelperEngine`1.WriteStream(TextWriter writer, IEnumerable`1 records, Int32 maxRecords) in FileHelperEngine.cs: line 558
// at FileHelpers.FileHelperEngine`1.WriteString(IEnumerable`1 records, Int32 maxRecords) in FileHelperEngine.cs: line 600
// at FileHelpers.FileHelperEngine`1.WriteString(IEnumerable`1 records) in FileHelperEngine.cs: line 591
// at FileHelpers.Tests.RunTimeClassesExtra.RuntimeClasses() in RunTimeClassExtra.cs: line 168
[Test]
public void RuntimeClasses()
{
var referenceType = typeof(Country);
var builder = new DelimitedClassBuilder(referenceType.Name, "\t");
var props = referenceType.GetProperties(BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.Instance);
foreach (var propertyInfo in props)
{
builder.AddField(propertyInfo.Name, propertyInfo.PropertyType);
}
var engine = new FileHelperEngine(builder.CreateRecordClass());
var source = new[] {new Country {Name = "ND"}};
var result = engine.WriteString(source);
Console.WriteLine(result);
}
public class Country
{
public string Name { get; set; }
}
@vcaraulean
Copy link
Author

I've got your idea. But I still have my problem unsolved :)
What if I want to customize generated output? DelimitedClassBuilder allowed me to skip some fields and set null values for others.

new FileHelperEngine(enumParam.First().GetType())

Is there any way to customize the result? In runtime, without using attributes...

I've looked trough the code & the docs and I have a sensation that FileHelpers isn't handling my requirements...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment