CsvHelper
CsvHelper copied to clipboard
Unable to load data from CSV file into an `IRealmObject`
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

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, LinkedList
1 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.ValidateHeaderT CsvReader.GetRecordsT+MoveNext() List1.ctor(IEnumerable1 collection) Enumerable.ToList[TSource](IEnumerable`1 source)
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.