Blazored / FluentValidation

A library for using FluentValidation with Blazor
https://blazored.github.io/FluentValidation/
MIT License
597 stars 85 forks source link

Addresses an issue with collection expressions in C# 12 #214

Closed Schaeri closed 8 months ago

Schaeri commented 9 months ago

We have identified an issue with Blazored/FluentValidation following the migration to .NET 8.0 and C# 12. Since we work with Fluxor, all our data is immutable. Moreover, we use the IReadOnlyList<> interface for lists throughout.

With the new C# 12 syntax for list initialization, a "standard" list/array is no longer created; instead, a type named "<>z__ReadOnlyArray" is generated. This new type neither possesses an Item property nor can it be converted into an object array. The code in the ToFieldIdentifier method within the EditContextFluentValidationExtensions class throws an exception as a result.

The following example demonstrates the problem (Note: the code must be compiled with C# 12, and we have tested with Visual Studio 17.9.0):

List<string> list = ["1", "2"];
IReadOnlyList<string> readonlyList = ["1", "2"];

var listHasItemProperty = list.GetType().GetProperties().Any(x => x.Name == "Item");
var readOnlyListHasItemProperty = readonlyList.GetType().GetProperties().Any(x => x.Name == "Item");

Console.WriteLine(listHasItemProperty); // True
Console.WriteLine(readOnlyListHasItemProperty); // False

var objects = readonlyList as object[];
Console.WriteLine(objects != null); // False

This pull request makes the code compatible again with IReadOnlyList and features from C# 12. We request a swift integration and release of a new Nuget package, as our validation currently does not work.

Thank you for your assistance.

Schaeri commented 8 months ago

Thank you very much for accepting the pull request. When will the package be released on NuGet?