Get Method Body as Text (edit)
Same Question: Get Method Body as Text - CodeProject
Best Solution: C# - Programatically insert line of code before end of method body - Stack Overflow
Log Method Name: .NET - Inserting code into C# methods right before compilation - Stack Overflow
- HTTP
- Authorization
- OAuth 2.0 (more)
- Azure DevOps
- Azure DevOps Services
- Personal Access Token
- Visual Studio Online
- RESTful APIs
Authorization using OAuth 2.0 - Azure DevOps | Microsoft Docs
Acessing Azure DevOps By Using PAT (c-sharpcorner.com)
Using REST APIs of TFS and Visual Studio Online | DotNetCurry
Use REST APIs to access Azure DevOps (formerly VSTS) | DotNetCurry
Samples:
Browse Code Examples (codegrepper.com)
https://www.codegrepper.com/code-examples/
GitHub - intuit/QuickBooks-V3-DotNET-SDK: .Net SDK for QuickBooks REST API v3 services
https://github.com/intuit/QuickBooks-V3-DotNET-SDK
using System; using System.Diagnostics; using System.IO; using System.Text; namespace ConsoleApp1 { /// <summary> /// Programatically insert line of code before end of method body /// </summary> public class Program { /// <summary> /// Programatically insert line of code before end of method body /// https://stackoverflow.com/questions/30108792/programatically-insert-line-of-code-before-end-of-method-body /// </summary> /// <param name="args"></param> private static void Main(string[] args) { const string methodName = "HelloWorld"; const string filePath = @"C:\Users\Nguyen Viet Manh\Desktop\FileHelper.cs"; string[] fileContent = System.IO.File.ReadAllLines(filePath, Encoding.UTF8); //const string newCode = "<--------INSERT CODE HERE -------->"; //string fileContents = System.IO.File.ReadAllText(filePath, Encoding.UTF8); ////this should return what you want //string newFileContents = Regex.Replace( // fileContents, // @"(?<=HelloWorld([^}]*})\s*?)", // Environment.NewLine + newCode + Environment.NewLine, // RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace); //System.IO.File.WriteAllText(filePath.Replace(".cs", "2.cs"), newFileContents, Encoding.UTF8); int positionStart = FindLineOfCodeMethod(filePath, methodName); int positionEnd = FindEndOfMethod(filePath, methodName); StringBuilder sb = new StringBuilder(); for (int i = positionStart; i <= positionEnd; i++) { sb.AppendLine(fileContent[i]); } string newFilePath = filePath.Replace(".cs", string.Format(".{0}.cs", methodName)); System.IO.File.WriteAllText(newFilePath, sb.ToString(), Encoding.UTF8); Console.WriteLine("Hello World!"); } private static int FindLineOfCodeMethod(string filename, string methodToFind) { string methodName = string.Empty; if (methodToFind != null) { methodName = string.Concat(" ", methodToFind.Trim(), "("); // "HelloWorld" => " HelloWorld(" } int countStartBracket = 0; int countEndBracket = 0; string line = ""; int lineNumber = -1; bool foundEnd = false; bool foundMethod = false; System.Action countBrackets = () => { if (line.Contains("{")) { countStartBracket++; } if (line.Contains("}")) { countEndBracket++; } //If we found the method and the start and end bracke- count is the same we found the end (and they are not zero) if (foundMethod && countStartBracket == countEndBracket && countStartBracket != 0 && countEndBracket != 0) { foundEnd = true; } }; //Pass the file path and file name to the StreamReader constructor using (StreamReader sr = new StreamReader(filename)) { //Continue to read until you reach end of file while (line != null) { lineNumber++; //Read next line, if it's null, we reached end of file and didn't find the place if ((line = sr.ReadLine()) == null) { return -1; } //If we haven't found the method yet, we check each line to see if it's on this line if (!foundMethod) { if (line.Contains(methodName)) { foundMethod = true; return lineNumber; } } //If it was or has been found we count brackets to determine the closing one if (foundMethod) { countBrackets(); } if (foundEnd) { Debug.WriteLine(lineNumber); return lineNumber; } } return -1; } } private static int FindEndOfMethod(string filename, string methodToFind) { int countStartBracket = 0; int countEndBracket = 0; string line = ""; int lineNumber = -1; bool foundEnd = false; bool foundMethod = false; System.Action countBrackets = () => { if (line.Contains("{")) countStartBracket++; if (line.Contains("}")) countEndBracket++; //If we found the method and the start and end bracke- count is the same we found the end (and they are not zero) if (foundMethod && countStartBracket == countEndBracket && countStartBracket != 0 && countEndBracket != 0) foundEnd = true; }; //Pass the file path and file name to the StreamReader constructor using (StreamReader sr = new StreamReader(filename)) { //Continue to read until you reach end of file while (line != null) { lineNumber++; //Read next line, if it's null, we reached end of file and didn't find the place if ((line = sr.ReadLine()) == null) return -1; //If we haven't found the method yet, we check each line to see if it's on this line if (!foundMethod) if (line.Contains(methodToFind)) foundMethod = true; //If it was or has been found we count brackets to determine the closing one if (foundMethod) countBrackets(); if (foundEnd) { Debug.WriteLine(lineNumber); return lineNumber; } } return -1; } } } } // **************************************** // // Input: C:\FileHelper.cs // // **************************************** //using System; //namespace MyNameSpace //{ // public class MyClass // { // public void HelloWorld(string somevalue) // { // //hello world // switch (somevalue) // { // case "Hello World": // //do something here // break; // default: // //HelloWorld // break; // } // //HelloWorld // //HelloWorld // HelloWorld(somevalue); // } // } //} // **************************************** // // Output: C:\FileHelper.HelloWorld.cs // // **************************************** //public void HelloWorld(string somevalue) //{ // //hello world // switch (somevalue) // { // case "Hello World": // //do something here // break; // default: // //HelloWorld // break; // } // //HelloWorld // //HelloWorld // HelloWorld(somevalue); //}