adoconnection / RazorEngineCore

.NET6 Razor Template Engine
MIT License
576 stars 85 forks source link

Fix AnonymousTypeWrapper cast failing on lists of structs #100

Closed DanielStout5 closed 2 years ago

DanielStout5 commented 2 years ago

This code:

using RazorEngineCore;

var eng = new RazorEngine();

var content = @"Your favourite colors are: 
@foreach(var color in Model.Colors) {
    @color.Name
}
";

var templ = eng.Compile(content);

var model = new
{
    Colors = new List<Color>()
    {
        new Color { Name = "Green" },
        new Color { Name = "Red" }
    }
};

var result = templ.Run(model);

Console.Write(result);

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

Currently throws this exception:

System.InvalidCastException: 'Unable to cast object of type 'System.Collections.Generic.List`1[Color]' to type 'System.Collections.Generic.IEnumerable`1[System.Object]'.'
adoconnection commented 2 years ago

thanks!