示例#1
0
        private void GoButton_Click(object sender, EventArgs e)
        {
            CustomCmdLineHost host = new CustomCmdLineHost();
            var session = new TextTemplatingSession();

            string templateFileName = TemplateList.SelectedItem.ToString();

            var sessionHost = (ITextTemplatingSessionHost)host;
            sessionHost.Session = session;

            foreach (string tableName in TableList.CheckedItems)
            {
                sessionHost.Session["tableName"] = tableName;
                //// Pass another value in CallContext:
                System.Runtime.Remoting.Messaging.CallContext.LogicalSetData("p2", "test");

                Engine engine = new Engine();
                host.TemplateFileValue = templateFileName;
                //Read the text template.
                string input = File.ReadAllText(templateFileName);
                //Transform the text template.
                string output = engine.ProcessTemplate(input, host);
                string outputFileName = tableName;
                outputFileName = Path.Combine(Path.GetDirectoryName(templateFileName), outputFileName);
                outputFileName = outputFileName + "1" + host.FileExtension;
                File.WriteAllText(outputFileName, output, host.FileEncoding);

                foreach (CompilerError error in host.Errors)
                {
                    Console.WriteLine(error.ToString());
                }
            }

            Console.ReadLine();
        }
示例#2
0
        public void Go()
        {
            string template = "<#@parameter type=\"System.Int32\" name=\"p1\"#>"
             + "<#@parameter type=\"System.String\" name=\"p2\"#>"
             + "Test <#=p1#> <#=p2#>";

            CustomCmdLineHost host = new CustomCmdLineHost();
            var session = new TextTemplatingSession();

            string templateFileName = "..\\..\\Templates\\TextTemplate2.tt";

            var sessionHost = (ITextTemplatingSessionHost)host;
            sessionHost.Session = session;

            sessionHost.Session["namespace"] = "mynamespace";
            //// Pass another value in CallContext:
            System.Runtime.Remoting.Messaging.CallContext.LogicalSetData("p2", "test");

            Engine engine = new Engine();
            host.TemplateFileValue = templateFileName;
            //Read the text template.
            string input = File.ReadAllText(templateFileName);
            //Transform the text template.
            string output = engine.ProcessTemplate(input, host);
            string outputFileName = Path.GetFileNameWithoutExtension(templateFileName);
            outputFileName = Path.Combine(Path.GetDirectoryName(templateFileName), outputFileName);
            outputFileName = outputFileName + "1" + host.FileExtension;
            File.WriteAllText(outputFileName, output, host.FileEncoding);

            foreach (CompilerError error in host.Errors)
            {
                Console.WriteLine(error.ToString());
            }

            Console.ReadLine();

            //Engine e = new Engine();

            //ITextTemplating t4 =  e.GetService(typeof(STextTemplating)) as ITextTemplating;
            //ITextTemplatingSessionHost host = t4 as ITextTemplatingSessionHost;
            //host.Session = host.CreateSession();
            //// Pass a value in Session:
            //host.Session["p1"] = 32;
            //// Pass another value in CallContext:
            //System.Runtime.Remoting.Messaging.CallContext.LogicalSetData("p2", "test");

            //// Process a small template inline:
            //string result = t4.ProcessTemplate("",
            //   "<#@parameter type=\"System.Int32\" name=\"p1\"#>"
            // + "<#@parameter type=\"System.String\" name=\"p2\"#>"
            // + "Test <#=p1#> <#=p2#>");
        }