Created
May 9, 2011 14:43
-
-
Save vcaraulean/962645 to your computer and use it in GitHub Desktop.
Test case for BadUsageException
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; } | |
} |
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
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