提取c#代碼檔案中的方法塊

來源:互聯網
上載者:User

標籤:

     此方法是取C#檔案裡面的方法塊,並刪除縮排符,感覺寫得還是比較容易懂的,所以收藏下,以便將來用到.

       private static string GetCodeBlock(string allCode, string blockName)        {            int blockStart = allCode.IndexOf(blockName, StringComparison.OrdinalIgnoreCase);                        if (blockStart == -1)                return "// " + blockName + " code not found";            blockStart = allCode.LastIndexOf(Environment.NewLine, blockStart, StringComparison.OrdinalIgnoreCase);            if (blockStart == -1)                blockStart = 0;            else                blockStart += Environment.NewLine.Length;            int pos = blockStart;            int braceCount = 0;            char c;            do            {                pos++;                c = allCode[pos];                switch (c)                {                    case ‘{‘:                        braceCount++;                        break;                    case ‘}‘:                        braceCount--;                        break;                }            } while (pos < allCode.Length && !(c == ‘}‘ && braceCount == 0));            int blockEnd = pos;                        string blockCode = allCode.Substring(blockStart, blockEnd - blockStart + 1);            return removeIndent(blockCode);        }        private static string RemoveIndent(string code)        {            int indentSpaces = 0;            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();        }

 

  

提取c#代碼檔案中的方法塊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.