简体   繁体   English

Razor引擎无法在C#上转换为VB.NET转换的动态/对象解析

[英]Razor Engine Fails on C# to VB.NET converted dynamic/object parsing

I am attempting to convert a C# implementation of an emailer class using the RazorEngine to parse a razor template and subsequently send an email into VB.NET. 我正在尝试使用RazorEngine转换电子邮件程序类的C#实现以解析剃刀模板,然后将电子邮件发送到VB.NET。 This works great in C#, but I am running into issues with the conversion of a C# dynamic type to a VB.NET 'object' type (which from what I can discern is the closest equivalent). 这在C#中效果很好,但是我遇到了将C#动态类型转换为VB.NET“对象”类型(从我可以看到的是最接近的等效对象)的问题。

For example, this code works wonderfully in C#: 例如,此代码在C#中可以很好地工作:

public static string GetEmailBody(string templatePath, dynamic model)
{
      var template = File.ReadAllText(templatePath);
      var body = Razor.Parse(template, model);
      return body;
}

In my conversion to VB.NET, I ended up with a function call that looks like this: 在转换为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

And my call to it looks like this: 我对此的呼叫如下所示:

RenderEmailBody("mytemplate.vbhtml", New With { .Var1 = "1", .Var2 = "2" })

When I run this, however, the following exception is thrown: 但是,当我运行此命令时,将引发以下异常:

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?)

When I inspect model while on a breakpoint, it seems to have Var1 and Var2 assigned correctly, but when I get to the call to Razor.Parse I continually have issues with it not interpreting my dynamic object correctly. 当我在断点上检查model时,似乎正确分配了Var1和Var2,但是当我调用Razor.Parse时, Razor.Parse问题,即它无法正确解释我的动态对象。

Am I doing something incorrectly here? 我在这里做错了什么吗? Or there an inherent incompatibility between the two types? 还是这两种类型之间固有的不兼容?

Object and dynamic are two completely separate things. Objectdynamic是两个完全独立的事物。 In the VB case, what you've done is unbox your model completely. 在VB情况下,您要做的是将模型完全拆箱。 They dynamic keyword in C# preserves the actual type without actually knowing it (humanly) at design time. 他们在C#中使用动态关键字保留了实际的类型,而在设计时并没有真正地(人工地)知道它。 However, when you use Object in this fashion you remove all actual reference to the type. 但是,以这种方式使用Object ,将删除对该类型的所有实际引用。 You would need to recast your model to its specific type in order to have access to .Var1 in this case. 在这种情况下,您需要将模型重铸为其特定类型,才能访问.Var1 Your best bet would be to adjust the signature of your method call either to have the specific type you're looking for or use an interface that your common models can implement that defines the .Var1 or .Var2 properties. 最好的选择是调整方法调用的签名,以具有要查找的特定类型,或者使用通用模型可以实现的定义.Var1.Var2属性的接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM