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

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

Conversation

Schaeri
Copy link

@Schaeri Schaeri commented Feb 20, 2024

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.

@pwelter34 pwelter34 merged commit a598bd9 into Blazored:main Mar 18, 2024
1 check passed
@Schaeri
Copy link
Author

Schaeri commented Mar 19, 2024

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

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

Successfully merging this pull request may close these issues.

2 participants