zoukankan      html  css  js  c++  java
  • [源码学习]RazorTemplateEngine.GenerateCodeCore之CreateParser()方法

    接上篇的GenerateCodeCore方法中

    View Code
                // Run the parser
                RazorParser parser = CreateParser();
                Debug.Assert(parser != null);
                parser.Parse(input, consumer);

    RazorParser parser = CreateParser();中的CreateParser代码如下:

    View Code
            protected internal virtual RazorParser CreateParser() {
                return new RazorParser(Host.DecorateCodeParser(Host.CodeLanguage.CreateCodeParser()),
                                                              Host.DecorateMarkupParser(Host.CreateMarkupParser())) {
                                                                  DesignTimeMode = Host.DesignTimeMode
                                                              };
            }

    Host是RazorTemplateEngine的属性,类型是RazorEngineHost

    View Code
            /// <summary>
            /// The RazorEngineHost which defines the environment in which the generated template code will live
            /// </summary>
            public RazorEngineHost Host { get; private set; }

    在构造函数中传入并赋值,传入的是RazorEngineHost的派生类WebPageRazorHost

            /// <summary>
            /// Constructs a new RazorTemplateEngine with the specified host
            /// </summary>
            /// <param name="host">The host which defines the environment in which the generated template code will live</param>
            public RazorTemplateEngine(RazorEngineHost host) {
                if (host == null) { throw new ArgumentNullException("host"); }
    
                Host = host;
            }

    传入的是RazorEngineHost的派生类WebPageRazorHost,在System.Web.WebPages.Razor命名空间中,这个类的GetCodeLanguage
    调用了RazorCodeLanguage.GetLanguageByExtension确定了CodeLanguage的实际类型,override MarkupParser CreateMarkupParser()确定了MarkupParser的实际类型。

    以下只研究一下CodeLanguage是CSharpRazorCodeLanguage的情况。

    传入参数时调用Host的两个方法Host.DecorateCodeParser和Host.DecorateMarkupParser,只是看起来两个判断是否为null用的方法

            /// <summary>
            /// Gets an instance of the code parser and is provided an opportunity to decorate or replace it
            /// </summary>
            /// <param name="incomingCodeParser">The code parser</param>
            /// <returns>Either the same code parser, after modifications, or a different code parser</returns>
            public virtual ParserBase DecorateCodeParser(ParserBase incomingCodeParser) {
                if (incomingCodeParser == null) { throw new ArgumentNullException("incomingCodeParser"); }
                return incomingCodeParser;
            }
    
            /// <summary>
            /// Gets an instance of the markup parser and is provided an opportunity to decorate or replace it
            /// </summary>
            /// <param name="incomingMarkupParser">The markup parser</param>
            /// <returns>Either the same markup parser, after modifications, or a different markup parser</returns>
            public virtual MarkupParser DecorateMarkupParser(MarkupParser incomingMarkupParser) {
                if (incomingMarkupParser == null) { throw new ArgumentNullException("incomingMarkupParser"); }
                return incomingMarkupParser;
            }

    第一个方法的参数Host.CodeLanguage.CreateCodeParser()即CSharpRazorCodeLanguage.CreateCodeParser()

            /// <summary>
            /// Constructs a new instance of the code parser for this language
            /// </summary>
            public override ParserBase CreateCodeParser() {
                return new CSharpCodeParser();
            }

    第二个方法的参数Host.CreateMarkupParser()即WebPageRazorHost.CreateMarkupParser()

            public override MarkupParser CreateMarkupParser() {
                return new HtmlMarkupParser();
            }

    RazorParser的构造函数直接就是两行赋值语句,下篇分析parser.Parse(input, consumer);吧

  • 相关阅读:
    java--面向抽象编程
    java--面向接口编程
    一个网页的测试用例
    浏览器Notwork XHR被隐藏了
    在element-ui label中设置空格
    数组对象排序
    js动态替换和插入字符串
    vue-quill-editor回显时移除焦点
    vue中监听页面是否有回车键按下
    vue-quill-edito 字体倾斜加粗无效
  • 原文地址:https://www.cnblogs.com/llcto/p/2922335.html
Copyright © 2011-2022 走看看