动软代码生成器在自动化生成.Net代码方面功能非常强大。在实际使用中,我也经常使用它的自定义模板功能,生成Js、html、php、java等代码。其模板里的函数与函数并不完全一致,现把模板常用代码写在这里,以方便查询使用。实际使用中每个项目要按自己框架情况进行定义。

<#@ template language="c#" HostSpecific="True" #>
<#@ output extension= ".js" #>
<#
    TableHost host = (TableHost)(Host); 
    string DbParaHead=host.DbParaHead;
    string DbParaDbType=host.DbParaDbType;
    string preParameter=host.preParameter;
    string ModelSpace = host.NameSpace+".Model."+ host.GetModelClass(host.TableName);
    string TableName =  host.GetModelClass(host.TableName);
    string ModuleName = TableName;
    string ActionName = TableName;
    if(TableName.IndexOf('_') > -1){
        string[] tempArray=TableName.Split('_');
        ModuleName=tempArray[0];
        ActionName=tempArray[1];
        if(tempArray.Length==3)ActionName += "_"+tempArray[2];
    }   
    ColumnInfo identityKey=host.IdentityKey;
    string returnValue = "void";
    if (identityKey!=null)
    {         
        returnValue = CodeCommon.DbTypeToCS(identityKey.TypeName);              
    }else if(2>1){
    }
    else{
        identityKey=host.Fieldlist[0];
    }
#>

字符串函数

String Abc=actionArr[2].Substring(0,1).ToUpper() + actionArr[2].Substring(1,actionArr[2].Length-1);
"CABc".ToLower()

字段遍历

<# for(int i=0;i< host.Fieldlist.Count;i++){
 ColumnInfo c = host.Fieldlist[i];
 //c.ColumnName   字段名称
 //CodeCommon.DbTypeToCS(c.TypeName)  字段类型
 //c.Length 字段长度
  String type=CodeCommon.DbTypeToCS(c.TypeName);
  if(type=="DateTime")type="String";
  else if(type=="int")type="Integer";
  else if(type=="string")type="String";
  else if(type=="decimal")type="Float";
#>
<#}#>

字段类型枚举:

CodeCommon.DbTypeToCS(c.TypeName)

  • DateTime
  • int
  • Boolean
  • string
  • decimal

更多推荐

动软代码生成器 常用函数