Support indexer use in InputBase
See original GitHub issueIs your feature request related to a problem? Please describe the problem.
I’m looking to create a group of InputCheckbox components, and have all of their values stored in Dictionary<object, bool>
where the value indicates if the checkbox is checked or not. Logically I tried something like:
@foreach(var kvp in dict) {
<InputCheckbox @bind-Value="dict[kvp.Key]"></InputCheckbox>
}
This doesn’t work though, as the indexer on Dictionary<>
is actually an InstanceMethodCallExpression1
, and thus runs afoul of FieldIdentifier
’s “MemberExpressions only rule”, throwing The provided expression contains a InstanceMethodCallExpression1 which is not supported. FieldIdentifier only supports simple member accessors (fields, properties) of an object.
Creating a wrapper around my dictionary boolean does solve the problem, however, as the expression is once more a member access:
class BooleanWrapper {
public bool Value { get; set; }
}
//...
Dictionary<object, BooleanWrapper> dict = new();
@foreach(var kvp in dict) {
<InputCheckbox @bind-Value="dict[kvp.Key].Value"></InputCheckbox>
}
Describe the solution you’d like
It doesn’t seem like it’d be too difficult to get FieldIdentifier
to support simple index access by adding some additional logic to ParseAccessor
that follows calls to get_Item
on arrays or dictionaries. I’d be happy to write the PR for it myself if there was any interest in such a thing.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
you BoolWrapper class works, you just need to but the object in a inline variable first. Like this
Done as part of https://github.com/dotnet/aspnetcore/pull/48990