Beispiel #1
0
        public GeneratorResults Generate(Type modelType,string template)
        {
            //准备临时类名,读取模板文件和Razor代码生成器
            string templateType = "YOYO.AspNetCore.ViewEngine.Razor.RazorViewTemplate";

            string templateTypeName = modelType != null ? string.Format(templateType + @"<{0}>", modelType.FullName) : templateType;

            var class_name = "c" + Guid.NewGuid().ToString("N");
            var host = new RazorEngineHost(new CSharpRazorCodeLanguage(), () => new HtmlMarkupParser())
            {
                DefaultBaseClass = templateTypeName,
                DefaultClassName = class_name,
                DefaultNamespace = "YOYO.AspNetCore.ViewEngine.Razor",
                GeneratedClassContext =
                                   new GeneratedClassContext("Execute", "Write", "WriteLiteral", "WriteTo",
                                                             "WriteLiteralTo",
                                                             "RazorViewTemplate.Dynamic", new GeneratedTagHelperContext())

            };
            host.NamespaceImports.Add("System");
            host.NamespaceImports.Add("System.Dynamic");
            host.NamespaceImports.Add("System.Linq");
            host.NamespaceImports.Add("System.Collections.Generic");

            var engine = new RazorTemplateEngine(host);
            return engine.GenerateCode(new StringReader(template)); ;
        }
Beispiel #2
0
 /// <summary>
 /// Constructs a new instance of the chunk generator for this language with the specified settings
 /// </summary>
 public override RazorChunkGenerator CreateChunkGenerator(
     string className,
     string rootNamespaceName,
     string sourceFileName,
     RazorEngineHost host)
 {
     return new RazorChunkGenerator(className, rootNamespaceName, sourceFileName, host);
 }
Beispiel #3
0
        /// <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(nameof(host));
            }

            Host = host;
        }
Beispiel #4
0
        /// <summary>
        /// Constructs the editor parser. One instance should be used per active editor. This
        /// instance <em>can</em> be shared among reparses, but should <em>never</em> be shared between documents.
        /// </summary>
        /// <param name="host">The <see cref="RazorEngineHost"/> which defines the environment in which the generated
        /// code will live. <see cref="RazorEngineHost.DesignTimeMode"/> should be set if design-time behavior is
        /// desired.</param>
        /// <param name="sourceFileName">The physical path to use in line pragmas.</param>
        public RazorEditorParser(RazorEngineHost host, string sourceFileName)
        {
            if (host == null)
            {
                throw new ArgumentNullException(nameof(host));
            }

            if (string.IsNullOrEmpty(sourceFileName))
            {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, nameof(sourceFileName));
            }

            Host = host;
            FileName = sourceFileName;
            _parser = new BackgroundParser(host, sourceFileName);
            _parser.ResultsReady += (sender, args) => OnDocumentParseComplete(args);
            _parser.Start();
        }
Beispiel #5
0
 /// <summary>
 /// Constructs the chunk generator.  Must return a new instance on EVERY call to ensure thread-safety
 /// </summary>
 public abstract RazorChunkGenerator CreateChunkGenerator(
     string className,
     string rootNamespaceName,
     string sourceFileName,
     RazorEngineHost host);