This method is to take a C # file inside the method block, and delete the indentation character, the feeling is still relatively easy to understand, so the collection, in order to use in the future.
Private Static stringGetcodeblock (stringAllCode,stringblockname) { intBlockstart =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; ElseBlockstart+=Environment.NewLine.Length; intpos =Blockstart; intBracecount =0; CharC; Do{pos++; C=Allcode[pos]; Switch(c) { Case '{': Bracecount++; Break; Case '}': Bracecount--; Break; } } while(POS < allcode.length &&!) (c = ='}'&& Bracecount = =0)); intBlockend =POS; stringBlockcode = allcode.substring (Blockstart, Blockend-blockstart +1); returnremoveindent (Blockcode); } Private Static stringRemoveindent (stringcode) { intIndentspaces =0; while(Code[indentspaces] = =' ') {indentspaces++; } StringBuilder Builder=NewStringBuilder (); string[] Codelines = code. Split (New string[] {environment.newline}, Stringsplitoptions.none); foreach(stringLineinchcodelines) { if(Indentspaces <Line . Length) Builder. Appendline (line. Substring (indentspaces)); ElseBuilder. Appendline (); } returnBuilder. ToString (); }
Extracting a method block from a C # code file