我想改善迄今提供的答案。 为什么? 他们都不会轻易让开发人员识别代码段来自哪个区域。为了公平起见,我准备了一个比较目前提供的三种解决方案的代码。 我的测试还证明不应该使用https://stackoverflow.com/users/418281/ankush-roy提供的解决方案,因为它也可以返回不属于某个区域的代码。
首先结果:---------
//****** Result:: Regex.Split(code, "#region")
public class TestRegionParsing
{
private void Test1()
{
//this code should not be read
}
ClassName.Test()
//Some method that does stuff
//some stuff
#endregion ClassName.Test()
ClassCName.Random()
public static void Test()
{
//Some more stuff
}
#endregion ClassCName.Random()
private void Test2()
{
//this code should not be read
}
ClassCName.Random()
public static void Test3()
{
//Some more stuff
}
#endregion
}
//****** Result:: GetCodeRegions(code)
//Some method that does stuff
//some stuff
public static void Test()
{
//Some more stuff
}
public static void Test3()
{
//Some more stuff
}
//****** Result:: Utils.GetRegionsFromCShapFile(csharpFinePath)
Region name converted:1.cs
//Some method that does stuff
//some stuff
Region name converted:2.cs
public static void Test()
{
//Some more stuff
}
Region name converted:3.cs
public static void Test3()
{
//Some more stuff
}
,是由两个代码解析的CS文件:
public class TestRegionParsing
{
private void Test1()
{
//this code should not be read
}
#region ClassName.Test()
//Some method that does stuff
//some stuff
#endregion ClassName.Test()
#region ClassCName.Random()
public static void Test()
{
//Some more stuff
}
#endregion ClassCName.Random()
private void Test2()
{
//this code should not be read
}
#region ClassCName.Random()
public static void Test3()
{
//Some more stuff
}
#endregion
}
现在我已经实现
代码
/// <summary>
/// For a given source code, it parses all regions and creates a dictionary where
/// Key=region name and Value=list of region's code lines.<para />
/// Known Issue: This method dos not work with regions within regions. <para />
/// </summary>
/// <param name="sourceCodeFileName">string - full source code .cs path</param>
/// <returns>Key=region name and Value=list of region's code lines.</returns>
public static Dictionary<string, List<string>> GetRegionsFromCShapFile(string sourceCodeFileName)
{
FileInfo f = new FileInfo(sourceCodeFileName);
if (f.Length > ONE_Gb)
{
throw new ArgumentOutOfRangeException(string.Format("File:{0} has size greater than {1}{2}", MAX_STORAGE, STORAGE_UNIT));
}
Dictionary<string, List<String>> regions = new Dictionary<string, List<string>>();
string[] readLines = File.ReadAllLines(sourceCodeFileName);
List<string> current_list = null;
foreach (string l in readLines)
{
if (l != null)
{
if (l.TrimStart().StartsWith("#region", StringComparison.CurrentCultureIgnoreCase))
{
string key = string.Format("{0}.cs", ExtractNumber(l.Trim()));
if (regions.ContainsKey(key))
{
throw new ArgumentException(string.Format("Duplicate named regions detected: {0}", l.Trim()));
}
regions[key] = new List<string>();
current_list = regions[key];
}
else
{
if (current_list != null) //ignores code read if it is not within a region
{
if (l.TrimStart().StartsWith("#endregion", StringComparison.CurrentCultureIgnoreCase))
{
current_list = null; //stops using the current list
}
else
{
//use current list
current_list.Add(l);
}
}
}
}
}
return regions;
}
我希望这可能有所帮助。