0


Want to add some console.writeln's to the first line of every method in my class
(c#, cs file in visual studio 2010). How to identify using Regex so i can search replace?

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 you

itsmatt
  • 31,265
  • 10
  • 100
  • 164
Gullu
  • 3,477
  • 7
  • 43
  • 70
  • Don't you use access modifiers on your methods? Searching for those would help a lot. – R. Martinho Fernandes Mar 18 '11 at 18:27
  • I need to search on ALL methods in my class no matter what the access modifier, no of parameters etc. mostly for code tracing. I am also looking at Postsharp.. – Gullu Mar 18 '11 at 18:39

3 Answers3

2

It'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.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • I guessed this but just wanted a confirmation from a regex master. i will now dive into the world of vs macros using resharper short cuts to get this done. thanks – Gullu Mar 18 '11 at 18:50
  • That's not even half of it, consider: `string s = "... void method() { ...";` or `/* ... void method() { ... */` – Bart Kiers Mar 18 '11 at 19:01
2

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.

xanadont
  • 7,493
  • 6
  • 36
  • 49
  • Objective is code tracing like postsharp. Entered this method type prints to console or log4net file/console appenders. Postsharp has issues when using mixed assemblies. Release version of my code will not have this junk. Just for debugging, tracing on gdi type solutions where the regular break point and watch stack trace is a pain. – Gullu Mar 18 '11 at 21:50
  • Yep, look into implementing a custom attribute. You'll have all the code in one place so it's easily changed as your needs grow & change. Having the code in one place also means it's easily removed for your release build. – xanadont Mar 19 '11 at 15:30
  • I added a link that should get you going here if you so desire. – xanadont Mar 19 '11 at 15:49
1

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.

Tergiver
  • 14,171
  • 3
  • 41
  • 68