CodeSmith生成的文件格式是带有BOM的UTF-8,如何在没有BOM的情况下将其更改为UTF-8?

时间:2016-01-18 18:45:11

标签: utf-8 byte-order-mark codesmith

我最近正在试用CodeSmith Generator Professional 7.1.0,有一件事让我感到不安,每当我用它生成一些文件时,文件格式总是带有BOM的UTF-8,我真的需要文件格式是没有BOM的UTF-8,但是我找不到任何设置或配置来执行此操作,我在Google上搜索了几天并且没有任何用处。任何想法都将不胜感激!

以下是模板代码:

<% @CodeTemplate Language = "C#"
TargetLanguage = "C#"
ResponseEncoding = "UTF-8"
Description = "Generates a very simple business object." %>
  <% @Property Name = "SourceDatabase"
Type = "SchemaExplorer.DatabaseSchema"
DeepLoad = "True"
Optional = "False"
Category = "01. Getting Started - Required"
Description = "Database that the tables views, and stored procedures should be based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, the Entire Database will then be generated." %>
  <% @Property Name = "SourceTable"
Type = "SchemaExplorer.TableSchema"
Category = "Context"
Description = "Table that the object is based on." %>
  <% @Assembly Name = "SchemaExplorer" %>
  <% @Assembly Name = "System.Data" %>
  <% @Import Namespace = "SchemaExplorer" %>
  <% @Import Namespace = "System.Data" %>
  <% @Assembly Name = "MySql.Data" %>
  <% @Import NameSpace = "MySql.Data.MySqlClient" %>
  <? php

//Language File for <%= SourceTable.Name.ToLower() %> [EN]
define('LANG_<% = SourceTable.Name.ToUpper() %>', '<% = GetTablebPerfix(SourceTable.Name) %> List'); <%
for (int i = 0; i < SourceTable.NonForeignKeyColumns.Count; i++) { %>
  define('LANG_<% = SourceTable.NonForeignKeyColumns[i].Name.ToUpper() %>', '<% = GetColumnComment(SourceTable.Columns[i].Name) %>'); <%
} %>
?>

< script runat = "template" >
  public string GetColumnComment(string _columnname) {
    DataSet _auditTables = new DataSet();
    string ConnectionString = "server=localhost; user id = root; password = xxxxx; database =" + SourceDatabase + "";
    MySqlConnection conn = new MySqlConnection(ConnectionString);
    MySqlDataAdapter adapter = new MySqlDataAdapter(conn.CreateCommand());
    adapter.SelectCommand.CommandText = "select * from information_schema.columns where (table_schema='" + SourceDatabase + "') and (table_name='" + SourceTable + "') and (column_name='" + _columnname + "')";
    adapter.Fill(_auditTables);
    string en_comment = "";
    if (_auditTables.Tables.Count > 0) {
      DataTable dt = _auditTables.Tables[0];
      if (dt.Rows.Count > 0) {
        string comments = dt.Rows[0][dt.Columns.Count - 1].ToString();
        if (comments.IndexOf('/') == -1) {
          en_comment = comments;
        } else {
          en_comment = comments.Split('/')[1];
        }
      }
    }

    conn.Close();
    return en_comment;

  }

public string GetTablebPerfix(string _tablename) {
  string _tbnamewhtprefix = "";
  if (_tablename.IndexOf('_') == -1) {
    _tbnamewhtprefix = _tablename;
  } else {
    _tbnamewhtprefix = _tablename.Split('_')[1];
  }

  return System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(_tbnamewhtprefix);
} < /script>

1 个答案:

答案 0 :(得分:2)

您可以在代码模板指令Encoding上设置两个属性来控制此属性,ResponseEncoding属性将控制模板的呈现和保存方式。

https://codesmith.atlassian.net/wiki/display/Generator/The+CodeTemplate+Directive