CodeSmith实用技巧(十三):使用CodeTemplateInfo对象

CodeSmith中使用CodeTemplateInfo可以获取当前模版的一些信息:

属性

返回值

CodeBehind   

Gets the full path to the code-behind file for the template (or an empty string if there is no code-behind file).

ContentHashCode

Gets the hash code based on the template content and all template dependencies.  

DateCreated

Gets the date the template was created.

DateModified

Gets the date the template was modified.  

Description

Gets the description.  

DirectoryName

Gets the name of the directory the template is located in.  

FileName

Gets the name of the template file.  

FullPath

Gets the full path to the template.  

Language

Gets the template language.  

TargetLanguage

Gets the target language.  

看一下一个具体的使用例子:

<%@ CodeTemplate Language="VB" TargetLanguage="Text" Description="Demonstrates CodeTemplateInfo." %>
<% DumpInfo() %>
<script runat="template">
Public Sub DumpInfo()
    Response.WriteLine(
"Template:        {0}", Me.CodeTemplateInfo.FileName)
    Response.WriteLine(
"Created:         {0}", Me.CodeTemplateInfo.DateCreated)
    Response.WriteLine(
"Description:     {0}", Me.CodeTemplateInfo.Description)
    Response.WriteLine(
"Location:        {0}", Me.CodeTemplateInfo.FullPath)
    Response.WriteLine(
"Language:        {0}", Me.CodeTemplateInfo.Language)
    Response.WriteLine(
"Target Language: {0}", Me.CodeTemplateInfo.TargetLanguage)
End Sub
</script>

执行该模版输出如下(环境不同,输出也不同):

Template:        CodeTemplateInfo.cst
Created:         
6/29/2005 8:54:19 PM
Description:     Demonstrates CodeTemplateInfo.
Location:        C:\Program Files\CodeSmith\v3.
0\SampleTemplates\Test\CodeTemplateInfo.cst
Language:        VB
Target Language: Text
posted @   TerryLee  阅读(4312)  评论(1编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示