Exemplo n.º 1
0
        private Stream ExecuteT4Template(string templateContent, Dictionary <string, string> propertyBag)
        {
            var host   = new CustomTemplateHost();
            var engine = new Engine();

            host.TemplateFileValue = "c:\\foo.t4";
            //string input = templateContent;

            var session = host.CreateSession();

            foreach (var property in propertyBag)
            {
                session.Add(property.Key, property.Value);
            }

            string output = engine.ProcessTemplate(templateContent, host);

            var stream       = new MemoryStream();
            var streamWriter = new StreamWriter(stream);

            streamWriter.Write(output);
            streamWriter.Flush();
            stream.Position = 0L;


            if (!host.Errors.HasErrors)
            {
                return(stream);
            }

            var exception = new T4TemplateException("T4 compilation error");

            foreach (var error in host.Errors)
            {
                exception.AddError(error.ToString());
            }

            throw exception;
        }
        private Stream ExecuteT4Template(string templateContent, Dictionary<string, string> propertyBag)
        {
            var host = new CustomTemplateHost();
            var engine = new Engine();
            host.TemplateFileValue = "c:\\foo.t4";
            //string input = templateContent;

            var session = host.CreateSession();
            foreach (var property in propertyBag)
            {
                session.Add(property.Key, property.Value);
            }

            string output = engine.ProcessTemplate(templateContent, host);

            var stream = new MemoryStream();
            var streamWriter = new StreamWriter(stream);
            streamWriter.Write(output);
            streamWriter.Flush();
            stream.Position = 0L;

            if (!host.Errors.HasErrors)
                return stream;

            var exception = new T4TemplateException("T4 compilation error");

            foreach (var error in host.Errors)
            {
                exception.AddError(error.ToString());
            }

            throw exception;
        }