我正在尝试使用 RazorEngine 转换电子邮件类的 C# 实现来解析剃刀模板,然后将电子邮件发送到 VB.NET。这在 C# 中效果很好,但是我遇到了将 C# 动态类型转换为 VB.NET“对象”类型的问题(据我所知,这是最接近的等价物)。
例如,这段代码在 C# 中运行良好:
public static string GetEmailBody(string templatePath, dynamic model)
{
var template = File.ReadAllText(templatePath);
var body = Razor.Parse(template, model);
return body;
}
在我转换到 VB.NET 时,我得到了一个如下所示的函数调用:
Private Shared Function RenderEmailBody(strTemplate As String, model As Object) As String
Dim template As String = File.ReadAllText(strTemplate)
Dim body As String = Razor.Parse(template, model)
Return body
End Function
我对它的调用如下所示:
RenderEmailBody("mytemplate.vbhtml", New With { .Var1 = "1", .Var2 = "2" })
但是,当我运行它时,会引发以下异常:
TemplateCompilationException was unhandled by user code
Unable to compile template. 'object' does not contain a definition for 'Var1' and no extension method 'Var1' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
当我model
在断点上检查时,它似乎正确分配了 Var1 和 Var2,但是当我调用到时,Razor.Parse
我不断遇到问题,它无法正确解释我的动态对象。
我在这里做错了什么吗?还是这两种类型之间存在固有的不兼容?