• CodeSmith中共享常用代码


    CodeSmith是个很好的模板化的代码生成器,可以生成任何文本的代码。CodeSmith本身就支持在模板里写脚本,比如 

    <script runat="template"> 
    public string SampleMethod() 
    { 
        return "abc"; 
     
    } 
    </script>

    但很多模板可能会有很多同样这种代码,就需要共享代码,以简洁模板。

    • 自定义模板类,然后在模板里引用这个类。

    文件test.cst.cs

    using System.ComponentModel; 
    using CodeSmith.Engine; 
     
    public class UtilityCodeTemplate : CodeTemplate 
    { 
        public string SampleMethod() 
        { 
            return "Hello! My method output!"; 
        } 
    }

    模板test.cst,注意Src和Inherits的设置

    <%@ CodeTemplate Language="C#" TargetLanguage="Text" Src="test.cst.cs" 
    Inherits="UtilityCodeTemplate" Debug="True" Description="Template description here." %>
     
    begin <%= SampleMethod() %> end
     
    • 自己生成一个程序集,在模板中通过引用

    打开Visual Studio, 添加一个类库项目。

    using System; 
    using System.Collections.Generic; 
    using System.Text; 
     
    namespace CodeSmith 
    { 
        public class Utility 
        { 
            public static string SampleMethod() 
            { 
                return "Test OK!"; 
            } 
        } 
    }

    生成程序集CodeSmith.dll, copy到C:"Program Files"CodeSmith"v4.1"AddIns目录下(看CodeSmith安装目录),重启一下CodeSmith Studio

    模板test.cst,引入程序集并添加命名空间

    <%@ CodeTemplate Language="C#" TargetLanguage="Text" Debug="True"%> 
    <%@ Assembly Name="CodeSmith" %> 
    <%@ Import Namespace="CodeSmith" %> 
     
    begin <%= Utility.SampleMethod() %> end
     
    • 使用include共享

    创建Test.cst.cs

    <script runat="template"> 
    public string SampleMethod() 
    { 
        return "Test OK!"; 
    } 
    </script>

    模板文件,使用include添加,include其实就是把文件引用进来

    <%@ CodeTemplate Language="C#" TargetLanguage="Text" Debug="True"%> 
     
    begin <%= SampleMethod() %> end 
     
    <!-- #include file="Test.cst.cs" -->
  • 相关阅读:
    APP Https双向认证抓包
    剖析XSS
    php连接mysql
    linux去掉某一字符开头的行
    memcached+php客户端
    memcached-repcached
    memcached+memadmin
    Linux GPT分区
    Linux查看文件夹大小
    linux挂载windwos共享文件
  • 原文地址:https://www.cnblogs.com/leeolevis/p/1383140.html
Copyright © 2020-2023  润新知