我正在使用RazorEngine进行电子邮件模板。我在模板中引入了Take()方法。我这样做是为了让作者可以决定他们想要多少记录,而无需我们直接更改代码中的任何C#。我尝试过将using语句添加到模板本身,也尝试过使用fluent配置和添加所需的名称空间,但我运气不佳。
错误:
'System.Collections.Generic.List<object>' does not contain a definition for 'Take'
以下是我对RazorEngine的流畅配置:
var config = new FluentTemplateServiceConfiguration(c =>
c.IncludeNamespaces(
"System",
"System.Linq",
"System.Collections",
"System.Collections.Generic"));
using (var service = new TemplateService(config))
{
//Razor.SetTemplateService(service);
dynamic dyModel = model;
var parsed = string.IsNullOrEmpty(cacheName)
? service.Parse(template, dyModel,null, cacheName)
: service.Parse(template, dyModel,null,null);
return parsed;
}
如果我故意错误地陈述了一个名称空间,我确实会收到一个错误,说它找不到它,所以我知道它正在处理配置数据,但尽管如此,我仍然会收到错误。
你知道我做错了什么吗?我传入了一个动态模型,它要么是List,要么上面有List。
所以这有点蹩脚,RazorEngine的wiki上有一张罚单说这已经修复了,所以要么我用错了,要么还没有修复,但这就是我必须做的,才能让它在razor文件中工作。
var topFive = ((List<dynamic>) Model.MyList).Take(5);