Skip to content

#each: member lookup throws InvalidCastException when items have different types #685

Closed

Description

@bardrop

Describe the bug

When #each walks items of different types, a plain {{name}} inside the block uses the member accessor of the first item's type for every later item.

  • If the first item is a Dictionary<string, object> and a later one is a number, string or list, rendering throws InvalidCastException: Unable to cast object of type 'System.Int64' to type 'System.Collections.Generic.Dictionary2[System.String,System.Object]'`.
  • If the first item is a number, later dictionaries render empty.

{{this.name}} works, because it goes through PathResolver.TryAccessMember, which builds the descriptor from the current instance. The bare {{name}} goes through BindingContext.TryGetVariable, which uses the cached Descriptor. Iterators reuse one context and only set innerContext.Value = value, so the cache is never dropped.

Expected behavior:

Same output as handlebars.js: each item is looked up by its own type. Items without the member render empty.

Test to reproduce

[Fact]
public void IteratorWithItemsOfDifferentTypes()
{
    var handlebars = Handlebars.Create();
    var render = handlebars.Compile("{{#each items}}[{{name}}]{{/each}}");
    object data = new
    {
        items = new object[]
        {
            new Dictionary<string, object> { ["name"] = "Erik" },
            42,
            new { name = "Helen" },
        }
    };

    var actual = render(data); // throws InvalidCastException
    Assert.Equal("[Erik][][Helen]", actual);
}

Other related info

The same happens when iterating the values of a dictionary ({{#each dict}}) whose values have mixed types, e.g. data parsed from JSON. Found by rendering random templates against handlebars.js 4.7.8 and comparing.

Activity

  1. rexm commented on Nov 21, 2026

    @rexm
    Member

    /harness adopt

  2. stencil-harness commented on Nov 21, 2026

    @stencil-harness
    Contributor

    I'm the agent managing intake of issues on Rex's behalf. I understand the report: inside #each, a bare {{name}} reuses the member descriptor cached for the first item's type, so items of other types throw InvalidCastException or render empty. I'll evaluate whether I can fix this autonomously and report back.

  3. stencil-harness commented on Nov 21, 2026

    @stencil-harness
    Contributor

    Fix is up in #691, and auto-merge is on, so it lands once the required checks pass.

    Handlebars-Net/Handlebars.Net: shipped as PR #691 (not merged yet)

    • BindingContext.Value now resets the cached ObjectDescriptor when the new value's runtime type differs from the previous one. When every item has the same type, the cache is kept and behavior doesn't change.
    • This is the same fix as fix: drop cached descriptor when the context value changes type #686 by @bardrop, applied on current master and credited in the commit. I also added one test case: int, then null, then a dictionary.

    Verification

    • New tests in IteratorTests.cs: IteratorWithItemsOfDifferentTypes, IteratorWithNumberBeforeDictionaryItem and ObjectIteratorWithValuesOfDifferentTypes. All three fail without the change (with InvalidCastException or wrong output) and pass with it.
    • Full test suite: 2273 passed, 0 failed.

    No release was published; I'd suggest a patch release once it merges.

  4. added a commit that references this issue on Nov 25, 2026
    7a016d5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions