Пример #1
0
        protected RazorPageGeneratorResult GenerateResult(RazorCodeDocument codeDocument)
        {
            if (codeDocument is null)
            {
                throw new ArgumentNullException(nameof(codeDocument));
            }

            codeDocument.SetImportSyntaxTrees(new[] { RazorSyntaxTree.Parse(RazorSourceDocument.Create(@"
                @using System
                @using System.Threading.Tasks
                @using System.Collections.Generic
                @using System.Collections
                ", fileName: null)) });
            var cSharpDocument = codeDocument.GetCSharpDocument();

            if (cSharpDocument.Diagnostics.Any())
            {
                var diagnostics = string.Join(Environment.NewLine, cSharpDocument.Diagnostics);
                throw new InvalidOperationException($"无法生成Razor页面代码,一个或多个错误发生:{diagnostics}");
            }

            return(new RazorPageGeneratorResult
            {
                ClassName = "TempleteRazorPageView",
                GeneratedCode = cSharpDocument.GeneratedCode,
            });
        }
Пример #2
0
        protected override void ExecuteCore(RazorCodeDocument codeDocument)
        {
            var options    = codeDocument.GetParserOptions() ?? _optionsFeature.GetOptions();
            var syntaxTree = RazorSyntaxTree.Parse(codeDocument.Source, options);

            codeDocument.SetSyntaxTree(syntaxTree);

            var importSyntaxTrees = new RazorSyntaxTree[codeDocument.Imports.Count];

            for (var i = 0; i < codeDocument.Imports.Count; i++)
            {
                importSyntaxTrees[i] = RazorSyntaxTree.Parse(codeDocument.Imports[i], options);
            }
            codeDocument.SetImportSyntaxTrees(importSyntaxTrees);
        }