CodeSmith ActiveSnippet

CodeSmith的ActiveSnippet 设置见 http://www.codesmithtools.com/usersguide/Using_ActiveSnippets.html#

填充属性模板


<%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="" Inherits="" Debug="False" Description="Template description here." %>
<%@ Property Name="typeandname" Type="System.String" Default="" Optional="True" Category="Strings" Description="" %>
<%@ Assembly Name="System.Data" %>
<%@ Import Namespace="System.Data" %>
<%
string[] ary0 = typeandname.Split(',');
foreach(string s in ary0)
{
    string[] ary = s.Split(' ');
%>
<%=ary[0]%> <%=(ary[1].ToLower())%> ;
public <%=ary[0]%> <%=ary[1]%>
{
    get
    {
        return <%=(ary[1].ToLower())%>;
    }
    set
    {
        <%=(ary[1].ToLower())%> = value;
    }
}
<%
}
%>

在 VS2005 中的Snippet Configuration中添加当前模板,并设置别名为"cp"

在类中写入如下语句 cp "int CID,string CNAME"  然后 通过右键菜单中的 Expand ActiveSnippet或连按两次 CTRL-E生成如下代码

       int cid;
       public int CID
       {
           get
           {
               return cid;
           }
           set
           {
               cid = value;
           }
       }
       string cname;
       public string CNAME
       {
           get
           {
               return cname;
           }
           set
           {
               cname = value;
           }
       }

当然了对于一些通用代码这样处理,效率很不错。

原文地址:https://www.cnblogs.com/rock_chen/p/926457.html