May 25, 2021 CodeSmith
2. Use GetFileName to modify the file name of the template output
When using code templates to generate code, the CodeSmith engine uses a number of objects behind it to help generate code, most commonly used
This article describes CodeTemplate, which represents code template objects handled by the CodeSmith engine and can interact directly with the CodeSmith engine through codeTemplate objects, such as:
In the previous example CodeSmith used tutorial (2): To write the first code template, we've modified the output file name with GetFileName, for example, defining a ClassName property in your template, and you can change the default file name of the template output to class name through GetFileName
<%@ Template Language="C#" TargetLanguage="Text" %>
<%@ Property Name="ClassName" Type="System.String" Default="ClassName" %>
This template shows off how to override the GetFileName method.
<script runat="template">
public override string GetFileName()
{
return ClassName + ".cs";
}
</script>
When you define the default values for a property, sometimes the default values for some properties may not be converted from String, and you can do so by overloading the ParseDefaultValue method, which is called when each property is processed in the CodeSmith engine, and if you overload the method, you can follow your own logic to handle the default value of the property.
CodeTemplate's Render method is called when the CodeSmith engine generates the final output, and you can modify the contents of the output or write the output to multiple files by overloading it. For example, in addition to generating the default output, the following code writes the output to two other files:
<%@ CodeTemplate Language="C#" TargetLanguage="Text"
Description="AddTextWriter Demonstration." %>
<%@ Import Namespace="System.IO" %>
//This template demonstrates using the AddTextWriter method
//to output the template results to multiple locations concurrently.
<script runat="template">
public override void Render(TextWriter writer)
{
StreamWriter fileWriter1 = new StreamWriter(@"test1.txt", true);
this.Response.AddTextWriter(fileWriter1);
StreamWriter fileWriter2 = new StreamWriter(@"test2.txt", true);
this.Response.AddTextWriter(fileWriter2);
base.Render(writer);
fileWriter1.Close();
fileWriter2.Close();
}
</script>
Note that the base of the base class is called. R ender, otherwise you won't output to the default file. This example Downloads
The CodeTemplate class defines the following events in which you can add automatic event handling when these events occur.
As Asp.Net Page objects, you can write directly to the output stream through CodeTemplate's Response property. Like what
<%@ CodeTemplate Language="C#" TargetLanguage="Text"
Description="This template demonstrates writing directly to the Response property" %>
<% RenderDirect(); %>
<script runat="template">
public void RenderDirect()
{
Response.WriteLine("Written directly to the Response property.");
Response.WriteLine("Hello " + System.Environment.UserName + "!");
}
</script>
Write two lines of text directly into the output stream. Response objects are typed codeTemplateWriter classes and are commonly used in the: