提取c#代码文件中的方法块

     此方法是取C#文件里面的方法块,并删除缩进符,感觉写得还是比较容易懂的,所以收藏下,以便将来用到.

       private static string GetCodeBlock(string allCode, string blockName)
{
int blockStart = allCode.IndexOf(blockName, StringComparison.OrdinalIgnoreCase); if (blockStart == -)
return "// " + blockName + " code not found";
blockStart = allCode.LastIndexOf(Environment.NewLine, blockStart, StringComparison.OrdinalIgnoreCase);
if (blockStart == -)
blockStart = ;
else
blockStart += Environment.NewLine.Length; int pos = blockStart;
int braceCount = ;
char c;
do
{
pos++; c = allCode[pos];
switch (c)
{
case '{':
braceCount++;
break; case '}':
braceCount--;
break;
}
} while (pos < allCode.Length && !(c == '}' && braceCount == )); int blockEnd = pos; string blockCode = allCode.Substring(blockStart, blockEnd - blockStart + ); return removeIndent(blockCode);
} private static string RemoveIndent(string code)
{
int indentSpaces = ;
while (code[indentSpaces] == ' ')
{
indentSpaces++;
} StringBuilder builder = new StringBuilder();
string[] codeLines = code.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
foreach (string line in codeLines)
{
if (indentSpaces < line.Length)
builder.AppendLine(line.Substring(indentSpaces));
else
builder.AppendLine();
} return builder.ToString();
}

  

上一篇:OSX 下搭建Asp.Net vNext的开发环境


下一篇:【转载】在Centos系统上采用二进制文件部署Node.js环境