From 26c7bb62477de7059f0687ff75c0d172d8e7c4aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raffael=20Sch=C3=A4rer?= Date: Tue, 20 Feb 2024 16:28:25 +0100 Subject: [PATCH] Addresses an issue with collection expressions in C# 12 regarding IReadOnlyList --- .../EditContextFluentValidationExtensions.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Blazored.FluentValidation/EditContextFluentValidationExtensions.cs b/src/Blazored.FluentValidation/EditContextFluentValidationExtensions.cs index 24fba59..7ab8ab8 100644 --- a/src/Blazored.FluentValidation/EditContextFluentValidationExtensions.cs +++ b/src/Blazored.FluentValidation/EditContextFluentValidationExtensions.cs @@ -199,6 +199,16 @@ private static FieldIdentifier ToFieldIdentifier(in EditContext editContext, in var indexerValue = int.Parse(nextToken); newObj = array[indexerValue]; } + else if (obj is IReadOnlyList readOnlyList) + { + // Addresses an issue with collection expressions in C# 12 regarding IReadOnlyList: + // Generates a <>z__ReadOnlyArray which: + // - lacks an Item property, and + // - cannot be cast to object[] successfully. + // This workaround accesses elements directly using an indexer. + var indexerValue = int.Parse(nextToken); + newObj = readOnlyList[indexerValue]; + } else { throw new InvalidOperationException($"Could not find indexer on object of type {obj.GetType().FullName}.");