public static string ProcessTemplate(string templateFile, IMarshalProperties objProperties) { string fileOutput = null; Microsoft.VisualStudio.TextTemplating.Engine e = new Microsoft.VisualStudio.TextTemplating.Engine(); using (TemplateGenerationHost host = new TemplateGenerationHost()) { SetCallContextData(objProperties); host.TemplateFileValue = templateFile; string fileContents = File.ReadAllText(templateFile); fileOutput = e.ProcessTemplate(fileContents, host); if (host.Errors.HasErrors) { fileOutput = string.Empty; foreach (var err in host.Errors) { fileOutput += "\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n"; fileOutput += err; } } //clear out the context data we used in the template SetCallContextData(objProperties, true); } return(fileOutput); }
public static string ProcessTemplate(string templateFile, List <KeyValuePair <string, string> > templateProperties) { string fileOutput = null; Microsoft.VisualStudio.TextTemplating.Engine e = new Microsoft.VisualStudio.TextTemplating.Engine(); using (TemplateGenerationHost host = new TemplateGenerationHost()) { foreach (var tp in templateProperties) { if (tp.Value != null) { CallContext.LogicalSetData(tp.Key.ToString(), tp.Value.ToString()); } } host.TemplateFileValue = templateFile; string fileContents = File.ReadAllText(templateFile); fileOutput = e.ProcessTemplate(fileContents, host); if (host.Errors.HasErrors) { fileOutput = string.Empty; foreach (var err in host.Errors) { fileOutput += "\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n"; fileOutput += err; } } //clear out the context data we used in the template //SetCallContextData(objProperties, true); } return(fileOutput); }