コード例 #1
0
        private static GeneratorResults GenerateCode(RazorTemplateEntry entry)
        {
            var host = new RazorEngineHost(new CSharpRazorCodeLanguage());

            host.DefaultBaseClass = string.Format("BtRetryService.Razor.RazorTemplating.RazorTemplateBase<{0}>", entry.ModelType.FullName);
            host.DefaultNamespace = "BtRetryService";
            host.DefaultClassName = entry.TemplateName + "Template";
            host.NamespaceImports.Add("System");
            host.NamespaceImports.Add("System.Data");
            host.NamespaceImports.Add("System.Collections");
            host.NamespaceImports.Add("System.Collections.Generic");
            host.NamespaceImports.Add("System.Linq");
            host.NamespaceImports.Add("System.Text");
            host.NamespaceImports.Add("System.Text.RegularExpressions");
            host.NamespaceImports.Add("System.Xml.Serialization");
            host.NamespaceImports.Add("System.Web");
            host.NamespaceImports.Add("System.Data.Entity");

            using (TextReader reader = new StringReader(entry.TemplateString))
                return(new RazorTemplateEngine(host).GenerateCode(reader));
        }
コード例 #2
0
        public void RegisterTemplate <TModel>(string templateName, string templateString)
        {
            if (compiledTemplateAssembly != null)
            {
                throw new InvalidOperationException("May not register new templates after compiling.");
            }
            if (templateName == null)
            {
                throw new ArgumentNullException("templateName");
            }
            if (templateString == null)
            {
                throw new ArgumentNullException("templateString");
            }

            templateItems[TranslateKey(typeof(TModel), templateName)] = new RazorTemplateEntry()
            {
                ModelType      = typeof(TModel),
                TemplateString = templateString,
                TemplateName   = "Rzr" + Guid.NewGuid().ToString("N")
            };
        }
コード例 #3
0
        public string GenerateOutput <TModel>(TModel model, string templateName)
        {
            if (compiledTemplateAssembly == null)
            {
                throw new InvalidOperationException("Templates have not been compiled.");
            }
            if (templateName == null)
            {
                throw new ArgumentNullException("templateName");
            }

            RazorTemplateEntry entry = null;

            try
            {
                entry = templateItems[TranslateKey(typeof(TModel), templateName)];
            }
            catch (KeyNotFoundException)
            {
                throw new ArgumentOutOfRangeException("No template has been registered under this model or name.");
            }

            var template = (RazorTemplateBase <TModel>)compiledTemplateAssembly.CreateInstance("BtRetryService." + entry.TemplateName + "Template");

            if (template == null)
            {
                throw new InvalidOperationException("Couldn't generate template");
            }

            template.Model = model;
            template.Execute();
            var output = template.Buffer.ToString();

            template.Buffer.Clear();
            return(output);
        }