2

我想知道是否有办法从我的预处理模板生成多个文件?

4

1 回答 1

2

Preprocessed templates return the whole generated code as a string when you call the "TransformText()" method. It's up to you in what file(s) that result is saved.

You could render tokens into that generated code wherever you want to start a new file, split the returned string and save each part in a separate file.

For example - if this was your preprocessed template:

<#@ template #>

// This output text goes to the first file

NEW_FILE_TOKEN

// this output text goes to the next file

And your calling code would be like this:

var myTemplateInstance = new MyTemplate();
var result = myTemplateInstance.TransformText();

var fileContents = result.Split("NEW_FILE_TOKEN");
for(int i = 0; i < fileContents.Count; i++)
{
    System.IO.File.WriteAllText("File" + i.ToString() + ".txt", fileContents[i]);
}

You'd end up with two files (File0.txt, and File1.txt), one containing the first comment line and the other one the second.

于 2013-02-27T13:41:30.203 回答