You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
#each: member lookup throws InvalidCastException when items have different types #685
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.
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.
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.
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.
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.
Activity
rexm commentedon Nov 21, 2026
/harness adopt
stencil-harness commentedon Nov 21, 2026
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 throwInvalidCastExceptionor render empty. I'll evaluate whether I can fix this autonomously and report back.stencil-harness commentedon Nov 21, 2026
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.Valuenow resets the cachedObjectDescriptorwhen 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.masterand credited in the commit. I also added one test case: int, then null, then a dictionary.Verification
IteratorTests.cs:IteratorWithItemsOfDifferentTypes,IteratorWithNumberBeforeDictionaryItemandObjectIteratorWithValuesOfDifferentTypes. All three fail without the change (withInvalidCastExceptionor wrong output) and pass with it.No release was published; I'd suggest a patch release once it merges.
fix: drop cached descriptor when the context value changes type