Skip to content

Commit

Permalink
Merge pull request #214 from Devigus-Engineering-AG/C#12FixCollection…
Browse files Browse the repository at this point in the history
…ExpressionsIssue

Addresses an issue with collection expressions in C# 12
  • Loading branch information
pwelter34 authored Mar 18, 2024
2 parents aaa5514 + 26c7bb6 commit a598bd9
Showing 1 changed file with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<object> 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}.");
Expand Down

0 comments on commit a598bd9

Please sign in to comment.