现在的位置: 首页 > 综合 > 正文

CodeSmith 4.0如何支持中文

2014年03月13日 ⁄ 综合 ⁄ 共 4296字 ⁄ 字号 评论关闭

第一步、在Tools-Options中选中Enable unicode support;
第二步、在模板文件的CodeTemplate声明中加上ResponseEncoding="UTF-8"属性。 

好久没有写东西了。今天动手用CodeSmith写了两个模板文件,解决了自己的一个实际问题:生成数据库中相应表对应的实体类。codeSmith的文档确实写得很好,像我这么不懂英文的人看他的文档都不觉得很吃力。
1、让CodeSmith Studio支持中文
   问题:在cs Studio中写模板文件时,如果使用了中文,保存之后再打开,中文就全部变成了“??”。
   解决方法:第一步、在Tools-Options中选中Enable unicode support;
             第二步、在模板文件的CodeTemplate声明中加上ResponseEncoding="UTF-8"属性。
2、让模板输出文本支持中文
public override void Render(TextWriter writer)
{
    //在这里定义StreamWriter的Encoding为UTF8即可
    StreamWriter fw = new StreamWriter(csName, false,System.Text.Encoding.UTF8);
    this.Response.AddTextWriter(fw);
    base.Render(writer);
    fw.Close();
}

其实满简单的,看代码就清楚了:
[color=Red]主模板文件:[/color]:aksCNIbatisMaster.cst

<%--
Name:aksCNIbatisMaster.cst
Author: Jim Lee,http://www.netgrid.cn
Description: 创建Ibastis.net使用的所有POJO类以及单元测试代码
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="c#" ResponseEncoding="UTF-8" Src="" Inherits="" Debug="False" Description="创建Ibastis.net使用的所有POJO类以及单元测试代码" %>
<%@ Property Name="NS" Type="String" Default="akscn" Optional="true" Category="代码定义" Description="定义命名空间名" Editor="" EditorBase="" Serializer="" %>
<%@ Property Name="ds" Type="SchemaExplorer.DatabaseSchema" Category="Database" Description="选择使用的数据库" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<%@ Register Name="POJO" Template="aksCNIbatisPojo.cst" MergeProperties="False" %>

<%
    POJO pj = new POJO();
    for (int i=0;i<ds.Tables.Count;i++){
        pj.csName=csfolder+"//"+ds.Tables[i].Name+".cs";
        pj.SourceTable=ds.Tables[i];
        pj.NS=this.NS;
        pj.RenderToFile(pj.csName,true);
        Response.WriteLine("正在创建"+ds.Tables[i].Name+"类……");
        }
    Response.WriteLine("完成!!");
   
%>
<script runat="template">
private string _csfolder = @"c:/temp";
[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor)),Category("代码定义"), Description("选择保存的文件夹")]
public string csfolder
{
      get {return _csfolder;}
      set {_csfolder= value;}
}
</script>

[color=Red]子模板文件:[/color]aksCNIbatisPojo.cst
<%--
Name:aksCNIbatisPojo.cst
Author: Jim Lee,http://www.netgrid.cn
Description:创建Ibastis.net使用的POJO类
--%>
<%@ CodeTemplate Language="C#" TargetLanguage="Text" ResponseEncoding="UTF-8" Debug="False" Description="创建Ibastis.net使用的Pojo类" %>
<%@ Property Name="NS" Type="String" Default="akscn" Optional="true" Category="代码定义" Description="定义命名空间名" Editor="" EditorBase="" Serializer="" %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Database" Description="选择使用的表" %>
<%@ Property Name="csName" Type="String" Default="sample.cs" Optional="False" Category="代码定义" Description="" Editor="" EditorBase="" Serializer="" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Assembly Name="CodeSmith.CustomProperties" %>
<%@ Assembly Name="System.Design" %>
<%@ Import Namespace="CodeSmith.CustomProperties" %>
<%@ Import Namespace="System.IO" %>
//-----------------------------------------------------------------------------------------
//  Name:    <%=SourceTable%>.cs
//    Desceiption:    <%=SourceTable%>实体类
//
//  Create By Jim Lee,http://www.netgrid.cn
//            <%= DateTime.Now.ToString() %>
// 
//    Copyright <%= DateTime.Now.Year %> by akscn.com
//-----------------------------------------------------------------------------------------

using System;

namespace <%=NS%>
{
   
    class <%=SourceTable%>
    {
        //定义<%=SourceTable%>类的私有字段
        <%writeField(SourceTable);%>
       
        //定义<%=SourceTable%>类的属性
        <%writeProperty(SourceTable);%>
    }
}
<script runat="template">
/*public override void Render(TextWriter writer)
{
    StreamWriter fw = new StreamWriter(csName, false,System.Text.Encoding.UTF8);
    this.Response.AddTextWriter(fw);
 
    base.Render(writer);
 
    fw.Close();
}
*/

private void writeField(SchemaExplorer.TableSchema ts){
    for (int i=0;i<ts.Columns.Count;i++)
    {
        Response.WriteLine(    "/t/t private "+ts.Columns[i].DataType+" _"+ts.Columns[i].Name);
        }
    }
private void writeProperty(SchemaExplorer.TableSchema ts){
    for (int i=0;i<ts.Columns.Count;i++)
    {
        Response.WriteLine("/t/t public "+ts.Columns[i].DataType+" "+ts.Columns[i].Name);
        Response.WriteLine("/t/t {");
        Response.WriteLine("/t/t/t get { return _"+ts.Columns[i].Name+"; } ");
        Response.WriteLine("/t/t/t set { _"+ts.Columns[i].Name+"= value; }");
        Response.WriteLine("/t/t }");
        Response.WriteLine();
        }   
    }
</script>

 

原帖地址:http://community.csdn.net/Expert/topic/5357/5357396.xml?temp=.2412836

【上篇】
【下篇】

抱歉!评论已关闭.