Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

MarcosMeli commented May 9, 2011

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

This comment has been minimized.

Copy link
Owner Author

vcaraulean commented May 9, 2011

@vcaraulean

This comment has been minimized.

Copy link
Owner Author

vcaraulean commented May 9, 2011

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

This comment has been minimized.

Copy link

MarcosMeli commented May 9, 2011

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

This comment has been minimized.

Copy link
Owner Author

vcaraulean commented May 10, 2011

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
You can’t perform that action at this time.