Csvhelper: Can't create CsvConfiguration instance on .net standard 2.0 with object initializer

Created on 18 Jan 2021  路  5Comments  路  Source: JoshClose/CsvHelper

Describe the bug
On 20.0.0 creating a CsvConfiguration with an Object initializer or using a "with" statement throws MissingMethod exception.

To Reproduce

  1. Create .Net Standard 2.0 libraray
  2. Reference CsvHelper 20.0.0
  3. Create Console project (.Net 5?)
  4. Create a CsvConfiguration inside the .Net Standard 2.0 library
  5. Call said method from the main project, or anywhere else

Expected behavior
Can create CsvConfiguration with an object initializer

Screenshots
image

Additional context
Source:
Temp.Lib:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <LangVersion>9.0</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="CsvHelper" Version="20.0.0" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="IsExternalInit" Version="1.0.0" PrivateAssets="all" />
  </ItemGroup>

</Project>
using CsvHelper.Configuration;
using System.Globalization;

namespace Temp.Lib
{
    public static class ConfigCreator
    {
        public static CsvConfiguration CreateConfig() => new(CultureInfo.InvariantCulture) { Delimiter = ";" };
    }
}



md5-c1177b2e3f8e3b3152d9b2f87ffae510



```csharp
using System;
using Temp.Lib;

namespace Temp
{
    public static class Program
    {
        public static void Main()
        {
            ConfigCreator.CreateConfig();

            Console.WriteLine("Config created successfully");
        }
    }
}

Suggestion
Maybe instead of a record with init only properties, use a record with get only properties and optional constructor parameters?

bug

All 5 comments

I was using a library to get the feature into older version. Doesn't seem to work. I just did it in the source and it seems to work now. I'll include this in the next version that will be out within the next couple of hours. I want to fix the other issues people are seeing with it.

I know, I have used the same nuget, but didn't do extensive testing with it. If you want to keep the CsvConfiguration a record you might want to try making it work. You could try not using the nuget for the init stuff but place the source code directry inside the project, and make the class internal

Yes, I tried that and your example works with it. :)

Ohh, sorry, I misread your comment. Thanks for the quick reactions, and the fix!

Was this page helpful?
0 / 5 - 0 ratings