Skip to content

Commit

Permalink
Merge pull request #100 from DanielStout5/FixAnonymousTypeWrapperCast
Browse files Browse the repository at this point in the history
Fix AnonymousTypeWrapper cast failing on lists of structs
  • Loading branch information
adoconnection authored Apr 6, 2022
2 parents ac0b735 + d6ca867 commit b65e99c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
18 changes: 18 additions & 0 deletions RazorEngineCore.Tests/TestCompileAndRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@ public async Task TestCompileAndRun_DynamicModel_PlainAsync()
Assert.AreEqual("Hello Alex", actual);
}

public struct Item
{
public string Name { get; set; }
}

[TestMethod]
public void TestCompileAndRun_StructList()
{
var eng = new RazorEngine();
var model = new
{
Items = new[] { new Item { Name = "Bob" }, new Item { Name = "Alice" } }
};
var temp = eng.Compile("@foreach(var item in Model.Items) { @item.Name }");
var result = temp.Run(model);
Assert.AreEqual("BobAlice", result);
}

[TestMethod]
public void TestCompileAndRun_DynamicModel_Nested()
{
Expand Down
6 changes: 2 additions & 4 deletions RazorEngineCore/AnonymousTypeWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
result = new AnonymousTypeWrapper(result);
}

bool isEnumerable = typeof(IEnumerable).IsAssignableFrom(type);

if (result is IDictionary dictionary)
{
List<object> keys = new List<object>();
Expand All @@ -58,9 +56,9 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
}
}
}
else if (isEnumerable && !(result is string))
else if (result is IEnumerable enumer && !(result is string))
{
result = ((IEnumerable<object>)result)
result = enumer.Cast<object>()
.Select(e =>
{
if (e.IsAnonymous())
Expand Down

0 comments on commit b65e99c

Please sign in to comment.