大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > ASP.NET技巧 > CodeSmith 使用教程(17) Merge策略

CodeSmith 使用教程(17) Merge策略(2)

关键词:CodeSmith使用教程  阅读(1171) 赞(42)

[摘要]本文主要介绍CodeSmith 使用教程(17) Merge策略,希望对你解决相关问题提供帮助。

然后通过CodeSmith项目为模板设置Merge策略:

选择InsertRegion策略, 然后设置要插入的RegionName。

生成后的代码如下:

public class InsertRegionsSample
{

    public void SomeCustomMethod()
	{
        // This is my custom code that I want to preserve.
        // I can make changes to it and my changes will
        // not be overwritten.
	}

	#region Sample Generated Region
// This region generated by CodeSmith on Saturday, 12 January 2013
	#endregion
}

可以看到CodeSmith只在Region 处插入代码,而该Region外的部分保持不变。

类似的PreserveRegions策略,代码和模板定义如下:
PreserveRegionsSample.cs

public class PreserveRegionsSample
{

#region "Custom Region 1"

	// This is a place holder for your custom code.
	// It must exist so that CodeSmith knows where
	// to put the custom code that will be parsed
	// from the target source file.
	// The region name is used to match up the regions
	// and determine where each region of custom code
	// should be inserted into the merge result.

#endregion

    public void SomeGeneratedMethod()
	{

        // This section and all other non-custom code
        // regions will be overwritten during each
        // template execution.
        // Current Date: Saturday, 12 January 2013
	}

#region "Custom Region 2"

    // The contents of this region will also be preserved
    // during generation.

#endregion

}

模板定义如下:

<%@ Template Language="C#" TargetLanguage="C#"    Description="Demonstrates using a PreserveRegions merge strategy in C#." %>
public class PreserveRegionsSample
{

#region "Custom Region 1"

	// This is a place holder for your custom code.
	// It must exist so that CodeSmith knows where
	// to put the custom code that will be parsed
	// from the target source file.
	// The region name is used to match up the regions
	// and determine where each region of custom code
	// should be inserted into the merge result.

#endregion

    public void SomeGeneratedMethod()
	{

        // This section and all other non-custom code
        // regions will be overwritten during each
        // template execution.
        // Current Date: <%= DateTime.Now.ToLongDateString() %>
	}

#region "Custom Region 2"

    // The contents of this region will also be preserved
    // during generation.

#endregion

}


相关评论