如何更改这些函数以返回字符串 [英] how to change these function to return a string
本文介绍了如何更改这些函数以返回字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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()
这篇关于如何更改这些函数以返回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文