Assuming this is just a one time thing and your source is "balanced" (equal amount of {
and }
in your sourcefiles) you can use Regex balancing for it.
This snippet just takes a file and checks inside it for any .. class .. { .. }
and returns them as String[]
.
You'll have to modify it to match AcceptVerbs(HttpVerbs.Post)
in the linq query at the end. Recursing through your directory and doing this for each .cs file I assume you have code for. finally you might also have to modify it a bit and use rx more/fewer times depending on the level of {}
you want to start looking in.
This test snippet was made in LinqPad you can download it and test it by setting Language to C# Program
, moving it to a console app that prints into a file or similar instead should be simple though.
void Main()
{
String data = String.Empty;
using (StreamReader reader = new StreamReader(@"/path/to/a/file.cs"))
data = reader.ReadToEnd();
CheckContents(data, "class").Dump();
}
// Define other methods and classes here
String[] CheckContents(String data, String pattern)
{
var rx = new Regex(@"{
(?>
{ (?<DEPTH> )
|
} (?<-DEPTH> )
|
[^}{]*
)*
(?(DEPTH)(?!))
}", RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
var rx2 = new Regex(@"[^{]*{
(?>
{ (?<DEPTH> )
|
} (?<-DEPTH> )
|
[^}{]*
)*
(?(DEPTH)(?!))
}", RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
var nameSpace = rx.Match(data).Value.Substring(1); // Remove { from namespace so we don't match the same thing over and over
var parts = rx2.Matches(nameSpace);
return (from Match part in parts where Regex.IsMatch(part.Value, pattern) select part.Value.Trim()).ToArray();
}