Regex for find all start of method lines in .cs files in visual studio
Before search replace:
void method1(int a)
{
}
After search replace:
void method1(int a)
{
console.writeln
}
I have resharper too if that helps. Alt up down arrows take you to next prev method start
thank youIt's probably possible to do this with regex for a very limited set of pre-defined method definitions, but probably impossible in the general case. That is, you could easily create a regular expression that matches "void method() {", and some simple variants. But things get complicated very quickly. Imagine this method:
public static Tuple<int, string, List<Tuple<int, string>>> DoSomething(<arbitrarily complex parameter list>)
{
}
You can use regex to get the individual tokens (i.e. "public", "static", etc.), but to determine if something is a method is going to require more parsing logic than you can do with regex only.
I would suggest that you use ReSharper or some similar tool that can identify the methods for you. Unless you really want to implement a large part of a C# parser.
Code smell? What's the larger objective here? Are you adding logging or adding analytics? To the casual programmer it looks like you're going about something that's probably common and already cleverly solved. I don't have much to go on but I'd say adding a custom attribute to your methods would be a better first start at this.
Edit: This type of approach is known as "Aspect Oriented Programming". Take a look at this sample for a great way to get what you're after.
Check out this article Document Your Code in No Time At All with Macros in Visual Studio. It deals with parsing code in a VS macro.
精彩评论