CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

iOS CSV Writing: InvalidOperationException: No public parameterless constructor found.

Open yalfers opened this issue 6 years ago • 5 comments

I saw this come up in a few issues, but I was unable to find any relating to writing a csv. Hopefully this isn't a complete duplicate..

Anyway, I'm writing to csv and it's working on all platforms outside of iOS.

using (StreamWriter writer = new StreamWriter(Application.persistentDataPath + "/output.csv"))
  using (CsvWriter csv = new CsvWriter(writer))
  {
       csv.Configuration.RegisterClassMap<MyClass.MyClassMap>();
       csv.WriteRecords(_myclass.MyClass as IEnumerable);
 }

Is there an alternative way to write to CSV, identical to how I am above? I have not been able to find a workaround, but I'm sure I'm missing something obvious.

yalfers avatar Nov 13 '18 19:11 yalfers

You can't use WriteRecords, WriteRecord, GetRecords or GetRecord because they use System.Linq.Expressions, which iOS doesn't allow. You'll need to loop the records and do csv.WriteField(obj.Property).

It looks like there is a new Expression<TDelegate>.Compile(bool) that may allow for this. https://docs.microsoft.com/en-us/dotnet/api/system.linq.expressions.expression-1.compile?view=netframework-4.7.2#System_Linq_Expressions_Expression_1_Compile_System_Boolean_

I'll have to look into that a little more and see if this will allow for this in iOS now.

JoshClose avatar Nov 13 '18 21:11 JoshClose

Looks like dotnet might just do it for you. https://stackoverflow.com/a/42033899/68499

Are you using the netstandard version of CsvHelper?

JoshClose avatar Nov 13 '18 22:11 JoshClose

I'm running off the Unity build, so I'm a little limited here.

yalfers avatar Nov 13 '18 23:11 yalfers

Off the Unity build of CsvHelper?

JoshClose avatar Nov 15 '18 17:11 JoshClose

You can't use WriteRecords, WriteRecord, GetRecords or GetRecord because they use System.Linq.Expressions, which iOS doesn't allow. You'll need to loop the records and do csv.WriteField(obj.Property).

It looks like there is a new Expression<TDelegate>.Compile(bool) that may allow for this. https://docs.microsoft.com/en-us/dotnet/api/system.linq.expressions.expression-1.compile?view=netframework-4.7.2#System_Linq_Expressions_Expression_1_Compile_System_Boolean_

I'll have to look into that a little more and see if this will allow for this in iOS now.

Does it support on ios now? It seems .net 8 support linq, but i got this issue

Insofan avatar Apr 12 '24 20:04 Insofan