Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't create CsvConfiguration instance on .net standard 2.0 with object initializer #1646

Closed
KuraiAndras opened this issue Jan 18, 2021 · 5 comments
Labels

Comments

@KuraiAndras
Copy link

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 = ";" };
    }
}

Temp console app:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\Temp.Lib\Temp.Lib.csproj" />
  </ItemGroup>

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

</Project>
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?

@JoshClose
Copy link
Owner

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.

@KuraiAndras
Copy link
Author

KuraiAndras commented Jan 18, 2021

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

@JoshClose
Copy link
Owner

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

@KuraiAndras
Copy link
Author

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

@JoshClose
Copy link
Owner

JoshClose commented Jan 18, 2021

Should be fixed in 21.0.0. https://www.nuget.org/packages/CsvHelper/21.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants