3

In a VS2010 Add-In in C#, how do you get the name and signature of the method your cursor is currently in?

I want to create an add-in that when run, gets the name and signature of the current method and then adds an "in" and "out" log message for that method.

Example:

Before:

public void TheMethod(string text)
{
...

return text;
}

After:

public void TheMethod(string text)
{
log.Trace("public void TheMethod( string text =" + text + " ) - in");

...

log.Trace("public void TheMethod( ... ) - out with text = " + text );

return text;
}

Are there any add-in tutorials/links that cover getting method info, seeing the top and bottom of a method, inserting text, etc? I've tried Googling and I'm not getting myc that's helpful.

Pang
  • 9,564
  • 146
  • 81
  • 122
Eric Brown - Cal
  • 14,135
  • 12
  • 58
  • 97
  • 2
    Not that this helps you now, but C# 5 is going to have special attributes that let you get method names, file names, and line numbers as arguments to methods. Pretty exciting stuff. – StriplingWarrior Nov 10 '11 at 17:38

2 Answers2

1

Addressing your logging requirement specifically, this is the sort of thing that Aspect Oriented Programming is suited for. PostSharp, for example, can do the kind of boundary actions you're looking for. See here for an example of how this is done with that framework.

mwilson
  • 1,255
  • 12
  • 17
1

You might want to consider something called an Aspect Oriented approach. See Aspect Oriented Programming: When to start using a framework? for a good start.

Community
  • 1
  • 1
Digbyswift
  • 10,310
  • 4
  • 38
  • 66