JoshClose / CsvHelper

Library to help reading and writing CSV files
http://joshclose.github.io/CsvHelper/
Other
4.65k stars 1.05k forks source link

Unable to load data from CSV file into an `IRealmObject` #2154

Open A9G-Data-Droid opened 1 year ago

A9G-Data-Droid commented 1 year ago

Describe the bug I have a custom object that I'm populating a list of from a CSV file using GetRecords<MyClass>. It's been working fine. Now I want to store that object in a Realm database, so I changed my object to implement : IRealmObject. Now I am receiving the error:

System.InvalidOperationException: 'This operation is only valid on generic types.'

To Reproduce Take any custom object that works with CSV reader and change the signature to implement : IRealmObject. Now it bork.

Expected behavior I would expect it to load the properties into the object just like it used to.

Screenshots image

Additional context The way RealmObjects work is Realm uses FodyWeaver to add magic dust to your object via dynamic code generation at compile time. You get a file named like MyClass_generated.cs which overrides GetValue, SetValue, adds some PropertyChangedEventHandler and SetManagedAccessor stuff. Any of those could be throwing it off. I don't know enough about CsvReader or Realms to be able to troubleshoot this.

Stack Trace

RuntimeType.GetGenericTypeDefinition() CollectionConverterFactory.Create(Type type, TypeConverterCache cache, ITypeConverter& typeConverter) TypeConverterCache.GetConverter(Type type) ClassMap.AutoMapMembers(ClassMap map, CsvContext context, LinkedList1 mapParents, Int32 indexStart) ClassMap.AutoMapMembers(ClassMap map, CsvContext context, LinkedList1 mapParents, Int32 indexStart) ClassMap.AutoMap(CsvContext context) CsvContext.AutoMap(Type type) CsvReader.ValidateHeader(Type type) CsvReader.ValidateHeader[T]() CsvReader.GetRecords[T]()+MoveNext() List1.ctor(IEnumerable1 collection) Enumerable.ToList[TSource](IEnumerable`1 source)

A9G-Data-Droid commented 1 year ago

My current workaround is having 2 objects with identical properties, one for loading from CSV and one for saving to the database. This is not ideal. I would rather have one object.

When looping through the properties and setting them I learned that I need to ignore properties added by Realm. This might be a clue except if we are just looking for properties that match the data, you wouldn't stumble into these other properties. They only exist if you are looping on the destination object properties.