CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

Non-Generic MemberMap Validate Throws Exception

Open ninjapiratica opened this issue 4 years ago • 1 comments

When trying to configure the Validate() for a MemberMap (the non-generic version), it throws an exception

Expression of type 'System.String' cannot be used for parameter of type 'CsvHelper.ValidateArgs' of method 'Boolean <.ctor>b__0_1(CsvHelper.ValidateArgs)' (Parameter 'arg0')

I believe that the problem lies in how the expression is being created. It should be typeof(ValidateArgs) instead of typeof(string).

https://github.com/JoshClose/CsvHelper/blob/dfd643d1b57a83b7ea5099e92e38f87723baac2c/src/CsvHelper/Configuration/MemberMap.cs#L214

It can be easily tested in an existing test by casting the Map() result to a MemberMap

private sealed class ValidateMap : ClassMap
{
	public ValidateMap()
	{
		((MemberMap)Map(m => m.Id)).Validate(args => !string.IsNullOrEmpty(args.Field));
		Map(m => m.Name);
	}
}

https://github.com/JoshClose/CsvHelper/blob/dfd643d1b57a83b7ea5099e92e38f87723baac2c/tests/CsvHelper.Tests/Reading/ValidateTests.cs#L96-L103

ninjapiratica avatar Jul 21 '21 21:07 ninjapiratica

@JoshClose This bug is blocking my company from updating CsvHelper and by proxy updating to .Net 6 . Explicitly listing all the properties is not a feasible workaround for us.

jons-aura avatar Feb 22 '22 13:02 jons-aura

Fixed in next release.

JoshClose avatar Oct 04 '22 22:10 JoshClose