Exemplo n.º 1
0
        private static IList <Namespace> ExtractNamespacesFromText(ref string fileContent)
        {
            IList <string>   namespaces = CSharpFileParser.GetNamespaces(fileContent);
            List <Namespace> output     = new List <Namespace>();

            for (int i = 0; i < namespaces.Count(); i++)
            {
                string current = namespaces[i];
                bool   isLast  = i == namespaces.Count() - 1;
                string end     = isLast ? "" : @"\s+namespace\s+";
                string pattern = $@"namespace\s+{current}\s+\{{(?<namespaceContent>[\w\W]+)\}}{end}";
                Match  match   = Regex.Match(fileContent, pattern);

                string    content       = match.Groups["namespaceContent"].Value.TrimEnd();
                Namespace namespaceItem = new Namespace
                {
                    Name          = current,
                    Imports       = CSharpFileParser.ExtractImportsFromText(ref content),
                    Content       = content,
                    HasMainMethod = CSharpFileParser.CheckHasMainMethod(content),
                };
                output.Add(namespaceItem);

                fileContent = Regex.Replace(fileContent, pattern, isLast ? "" : "namespace ");
            }

            return(output);
        }
Exemplo n.º 2
0
        public static CSharpFileInfo ParseFile(string filePath)
        {
            string fileText = File.ReadAllText(filePath);

            return(new CSharpFileInfo
            {
                Name = Path.GetFileName(filePath),
                FullPathName = filePath,
                HasMainMethod = CSharpFileParser.CheckHasMainMethod(fileText),
                NamespacesContent = CSharpFileParser.ExtractNamespacesFromText(ref fileText),
                Imports = CSharpFileParser.ExtractImportsFromText(ref fileText),
            });
        }