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}.");