/// <summary> /// 获取类内容和类名 /// </summary> /// <param name="filePath">cs文件地址</param> /// <returns></returns> private List<KeyValuePair<string, StringBuilder>> GetClass(string filePath) { // 读取文件内容 string[] fileContent = File.ReadAllText(filePath).Split("\r\n"); int tempCount = 0; List<KeyValuePair<string, StringBuilder>> tempClass = new List<KeyValuePair<string, StringBuilder>>(); bool isNameSpace = false; StringBuilder sb = new StringBuilder(); bool startClass = false; string className = null, tempStr; int startIndex, endIndex; for (int i = 0; i < fileContent.Length; i++) { if (fileContent[i].TrimStart().StartsWith("namespace")) { //跳过命名空间下面那行 isNameSpace = true; i++; continue; } //命名空间开始再判断 if (!isNameSpace) continue; sb.AppendLine(fileContent[i]); if (fileContent[i].Contains("class")) { startIndex = fileContent[i].IndexOf("class ") + 6; tempStr = fileContent[i].Substring(startIndex); endIndex = tempStr.IndexOf(" "); if (endIndex == -1) className = tempStr; else className = tempStr.Substring(0, endIndex); } if (fileContent[i].TrimStart().StartsWith("{")) { tempCount++; startClass = true; } else if (fileContent[i].TrimStart().StartsWith("}")) { tempCount--; } if (tempCount == 0 && startClass) { startClass = false; tempClass.Add(new KeyValuePair<string, StringBuilder>(className, sb)); sb = new StringBuilder(); } } return tempClass; }
标签:endIndex,startClass,C#,tempCount,获取,tempStr,fileContent,new,类名 From: https://www.cnblogs.com/cvol/p/17600777.html