Razor 使用匿名对象的RazoEngine RuntimeBinderException

Razor 使用匿名对象的RazoEngine RuntimeBinderException,razor,razorengine,Razor,Razorengine,我在模板解决方案中使用RazorEngine。我有将匿名对象用于模型数据。守则: string template = @" Name: @Model.name @foreach(var person in @Model.People) { <row> <column header=""Name"" width=""5cm"">@perso

我在模板解决方案中使用RazorEngine。我有将匿名对象用于模型数据。守则:

        string template = @"
            Name: @Model.name
            @foreach(var person in @Model.People) {
                <row>
                    <column header=""Name"" width=""5cm"">@person.Name</column>
                    <column header=""Age"" width=""5cm"">@person.Age</column>
                </row>
            }";

        var personList = new List<object>();
        personList.Add(new { Name = "Bob", Age = 25 });
        personList.Add(new { Name = "Peter", Age = 30 });

        var model = new { name = "Henry", People = personList };

        string result = Razor.Parse(template, model);  
字符串模板=@”
名称:@Model.Name
@foreach(var person in@Model.People){
@人名
@人.年龄
}";
var personList=新列表();
添加(新的{Name=“Bob”,年龄=25});
添加(新的{Name=“Peter”,年龄=30});
var model=new{name=“Henry”,People=personList};
字符串结果=Razor.Parse(模板、模型);
我得到一个RuntimeBinderException('object'不包含'Name'的定义)

有人能帮我吗

谢谢大家!

在我看来,具有内部属性的匿名类型是一个糟糕的.NET framework设计决策

这里有一个快速而好的扩展来解决这个问题,即通过立即将匿名对象转换为ExpandooObject


请参考下面的答案:。

您是否尝试过使用
动态
类型,因为匿名类型是内部生成的,所以活页夹并不真正“了解”它本身:
动态personList=new List()。。。动态模型=新{name=“Henry”,人物=个人列表}