Skip to content

Instantly share code, notes, and snippets.

@vcaraulean
Created May 9, 2011 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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; }
}
@MarcosMeli
Copy link

The DelimitedClassBuilder is for dynamic created records, if you have the record at compile type you must use FileHelperEngine like this:

        var engine = new FileHelperEngine<Country>();
        var source = new[] {new Country {Name = "ND"}};
        var result = engine.WriteString(source);
        Console.WriteLine(result);

[DelimitedRecord("\t")]
public class Country
{
public string Name { get; set; }
}

@vcaraulean
Copy link
Author

vcaraulean commented May 9, 2011 via email

@vcaraulean
Copy link
Author

This sample is "simplified". The real requirement is a bit different: I'm receiving a IEnumerable from my data source and I'm trying to write it to a file under a concrete structure, this case Country.

@MarcosMeli
Copy link

I got it now, you must get the first object of the IEnumerable and get his type and later call the library with the enumerable, for example (using Linq)

// create an engine for the type of the objects (You must check for non empty)
var engine = new FileHelperEngine(enumParam.First().GetType());
// write all values
var result = engine.WriteString(enumParam);
Console.WriteLine(result);

Hope this helps :)

Edit: The original problem is that the ClassBuilder is creating a completely new type called "Country" but is not the same that the Country {Name = "ND"} that you are creating, so the library fails because it expect an object of the dynamic created class Country

@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