简体   繁体   English

剃刀模板委托,内部如何工作?

[英]razor template delegate, how the inside works?

I'm reading this excellent article regarding razor template delegate. 我正在阅读有关剃刀模板委托的出色文章。 http://www.prideparrot.com/blog/archive/2012/9/simplifying_html_generation_using_razor_templates http://www.prideparrot.com/blog/archive/2012/9/simplifying_html_generation_using_razor_templates

While I understand how it's used, as in 虽然我了解它的用法,例如

Func<dynamic, HelperResult> variable = @<var>@item.ProductName</var>

My question is, how exactly razor engine translated " @<var>@item.ProductName</var> " into a delegate in the background? 我的问题是,剃须刀引擎如何将“ @<var>@item.ProductName</var> ”准确地转换为后台的委托? As in

Func<dynamic, HelperResult> variable = delegate(dynamic x)
{
   (what goes on in here?)
}

is @item a reserved keyword that razor parses out? @item是剃刀解析出的保留关键字吗? Can it be used in any other convention? 可以在其他任何约定中使用它吗? say @column or @row or any other ways? @column@row还是其他方式?

Thanks a lot. 非常感谢。 Like I said, i'm more interested in how razor view engine translated the template statements into actual code in the background. 就像我说的那样,我对razor视图引擎如何将模板语句转换为后台的实际代码更感兴趣。

[Edit]. [编辑]。 Thanks to Brad for pointing out Andrew's article. 感谢布拉德指出安德鲁的文章。 so above statement " @<var>@item</var> " will translate into 因此,上面的语句“ @<var>@item</var> ”将转换为

Func<dynamic, HelperResult> variable = delegate(dynamic item)
{
     return new Microsoft.WebPages.Helpers.HelperResult(__writer => {
     @__writer.Write(" ");
     @__writer.Write("<var>");
     @__writer.Write(item.ProductName);     <--- what's happening here?
     @__writer.Write("</var>");
}

So I see razor automatically parses out @<var> and </var> into separate strings and such, my question in regards to "item.ProductName" is..suppose "item" is a "Proudct" type, then is the following what razor is trying to do? 因此,我看到razor自动将@<var></var>解析为单独的字符串,这样,关于“ item.ProductName”的问题是..假设“ item”是“ Proudct”类型,则如下剃刀想做什么?

First, razor parses "@item.ProductName" separated by comma ".", get "item" and "ProductName". 首先,剃刀解析“ @ item.ProductName”,以逗号“。”分隔,获取“ item”和“ ProductName”。

Then because of the "dynamic" parameter, in the background, .NET will attempt to find the value of the property "ProductName" of the item "Product"? 然后,由于存在“动态”参数,.NET将在后台尝试查找项目“产品”的属性“产品名称”的值?

Thanks 谢谢

Never mind. 没关系。 I don't know why i didn't make the connection. 我不知道为什么我没有建立连接。

I asked another question in regards to DynamicObject DynamicObject? 我问了关于DynamicObject DynamicObject的另一个问题 How does the following code work? 以下代码如何工作? and @Alxandr explained this regarding "dynamic". @Alxandr解释了有关“动态”的问题。 So essentially, it becomes 所以从本质上讲,它变成了

dynamic item = new Product(...);
String ProductName = item.ProductName;

So in essence, "dynamic" parameter in the background use CSharpGetMemberBinder and through reflection, figure out the "ProductName" of the object "Product". 因此,实质上,后台的“ dynamic”参数使用CSharpGetMemberBinder并通过反射来找出对象“ Product”的“ ProductName”。

razor template has a pretty brilliant design 剃刀模板的设计非常漂亮

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

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