<p>TemplateBase.cs是CodeSmith的核心,要编写高质量的模板必须先要了解其中的内容
这里我简单说一下:要添加一个MyTest.cst模板需要在TemplateBase.cs中注意修改的3个地方:
1.</p>
<pre class="csharp" name="code"> #region Enumerations
public enum ObjectType
{
<font color="#ff0000"> MyTest,
</font>
EditableRoot,
EditableRootList,
EditableChild,
EditableChildList,
EditableSwitchable,
NameValueList,
ReadOnlyRoot,
ReadOnlyRootList,
ReadOnlyChild,
ReadOnlyChildList
}</pre>
2.
<pre class="csharp" name="code"> public class TemplateHelper
{
private TemplateHelper() { }
public static bool IsObjectType(ICodeTemplateInfo info)
{
switch (info.FileName.ToLower())
{
<font color="#ff0000">case "mytest.cst":
</font>3.<pre class="csharp" name="code"> public static ObjectType ToObjectType(ICodeTemplateInfo info)
{
switch (info.FileName.ToLower())
{
<font color="#ff0000">case "mytest.cst": return ObjectType.MyTest;
</font>
case "editableroot.cst": return ObjectType.EditableRoot;
case "editablerootlist.cst": return ObjectType.EditableRootList;</pre>
<pre class="csharp" name="code">否则生成的时候会报错,提示不能生成业务类型</pre>
</pre>
这里我简单说一下:要添加一个MyTest.cst模板需要在TemplateBase.cs中注意修改的3个地方:
1.</p>
<pre class="csharp" name="code"> #region Enumerations
public enum ObjectType
{
<font color="#ff0000"> MyTest,
</font>
EditableRoot,
EditableRootList,
EditableChild,
EditableChildList,
EditableSwitchable,
NameValueList,
ReadOnlyRoot,
ReadOnlyRootList,
ReadOnlyChild,
ReadOnlyChildList
}</pre>
2.
<pre class="csharp" name="code"> public class TemplateHelper
{
private TemplateHelper() { }
public static bool IsObjectType(ICodeTemplateInfo info)
{
switch (info.FileName.ToLower())
{
<font color="#ff0000">case "mytest.cst":
</font>3.<pre class="csharp" name="code"> public static ObjectType ToObjectType(ICodeTemplateInfo info)
{
switch (info.FileName.ToLower())
{
<font color="#ff0000">case "mytest.cst": return ObjectType.MyTest;
</font>
case "editableroot.cst": return ObjectType.EditableRoot;
case "editablerootlist.cst": return ObjectType.EditableRootList;</pre>
<pre class="csharp" name="code">否则生成的时候会报错,提示不能生成业务类型</pre>
</pre>
本文介绍了如何通过修改CodeSmith核心文件TemplateBase.cs来定制自己的模板。重点讲解了添加新模板MyTest.cst所需的三个关键步骤,并强调了这些修改对于模板正常工作的必要性。
2157

被折叠的 条评论
为什么被折叠?



