正在尝试使用codeSmith这个工具进行代码生成工作,经过试验知道了如何将代码直接生成到文件中。
 
关键是这几行
 
<%@ Property Name = "OutputDirectory" Type="String" Category="General" Description="The directory to output the results to." %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
 
可以运行的代码如下:
 
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="" Inherits="OutputFileCodeTemplate" Debug="False" Description="C# codeMaker" %>
<%@ Property Name="NameSpace" Type="String" Category="Context" Description="The namespace to use for this class"%>
<%@ Property Name="ClassName" Type="String" Optional="False" Category="Context" Description="The name of the class to generate" %>
<%@ Property Name="DevelopersName" Type="String" Optional="False" Category="Context" Description="The name to include in the comment header"%>
<%@ Property Name = "OutputDirectory" Type="String" Category="General" Description="The directory to output the results to." %>
<%@ Assembly Name="CodeSmith.BaseTemplates" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="CodeSmith.BaseTemplates" %>
///////////////////////////////////////////////////////////////////////////////////////
// File: <%=ClassName%>.cs
// Description: Enter summary here after generation.
// ---------------------
// Copyright ? <%= DateTime.Now.Year %> Our Client
// ---------------------
// History
// <%= DateTime.Now.ToShortDateString() %> <%= DevelopersName%> Original Version
///////////////////////////////////////////////////////////////////////////////////////
using System;
namespace <%=NameSpace%>
{
/// <summary>
/// Summary description for <%=ClassName %>.
/// </summary>
public class <%=ClassName%>
{
//
// TODO: Add constructor logic here
//
}
}