如何更改这些函数以返回字符串
问题描述:
foreach (var item in projectToWrite)
{
// Create an output file
StringBuilder builder = new StringBuilder();
msbuildFile.WriteLine(GetMSBuildFileHeader());
msbuildFile.WriteLine(GetMSBuildFileItem(item, compilerOutputPath));
// Add the Details
if (item.Type == ProjectTypes.CPP)
{
//WriteBuildFileFooterCPP(msbuildFile);
WriteBuildFileFooterCPP(msbuildFile);
}
else if (item.Type == ProjectTypes.Cs)
{
//builder.Append(ProjectTypes.Cs);
WriteBuildFileFooterCS(msbuildFile);
}
else if (item.Type == ProjectTypes.VB)
{
//builder.Append(ProjectTypes.VB);
WriteBuildFileFooterVB(msbuildFile);
}
msbuildFile.WriteLine(GetMSBuildFileFooter());
}
msbuildFile.Flush();
msbuildFile.Close();
}
答
用AppendLine指令替换WriteLines:
Replace the WriteLines with AppendLine instructions:
StringBuilder builder = new StringBuilder();
foreach (var item in projectToWrite)
{
// Create an output file
builder.AppendLine(GetMSBuildFileHeader());
builder.AppendLine(GetMSBuildFileItem(item, compilerOutputPath));
以此类推.
注意:我将字符串生成器移到了循环之外,因此它会为所有字符串组装数据.
然后,最后:
And so forth.
NOTE: I moved the string builder outside the loop, so it assembles data for all strings.
Then, at the end:
return builder.ToString();
如果我正在阅读正确无误,在我看来,您只需要取消注释字符串生成器的附加行,然后将此循环放入返回字符串的函数中-您的最后一行将是
If I''m reading this correctly then it seems to me that you just need to uncomment your string builder append lines and put this loop into a function that returns a string - your last line would then be
return builder.ToString()