mtilhan
mtilhan

Reputation: 248

How can I get the definition of a function from CodeFunction?

I do have a CodeFunction object from EnvDTE namespace. I do want to get the definition of it; e.g.:

private void MenuItemCallback(object sender, EventArgs e)
{
    MessageBox.Show("Try");
}

I would like to get first line as string.

What I have tried until now,

1 ) Try to make a string by getting CodeFunction's Type (return type) and Parameters then in a loop add them to a string. However, parameter types' names become like "System.UInt32" etc. which I don't want to. Also a problem with this, it may not take ref Guid pguidCmdGroup as fully. I am afraid of skipping ref at this.

2 ) I tried to use functions of CodeFunction but all I could get was simple name of it.

3 ) I tried to write from starting point and ending point of the CodeFunction but couldn't find a way to turn two TextPoint to string and as I realized ending point is not the ending of the definition but the function it self which I don't want to.

How can I get just simply private void MenuItemCallback(object sender, EventArgs e) or MenuItemCallback(object sender, EventArgs e)?

Thanks for all your help.

Upvotes: 2

Views: 664

Answers (1)

Jack Griffin
Jack Griffin

Reputation: 1292

You must use GetStartPoint() and GetEndPoint() :
Read the full source of the function and then cut off the code before the first open curly brace.

// Retrieve the source code for the function.
TextPoint start = codeFunction.GetStartPoint(vsCMPart.vsCMPartHeader);
TextPoint finish = codeFunction.GetEndPoint();

string fullSource = start.CreateEditPoint().GetText(finish);

// Find the first open curly brace
int openCurlyBracePos = fullSource.IndexOf('{');

string declaration = String.Empty;
if (openCurlyBracePos > -1) {
    declaration = fullSource.Substring(0, openCurlyBracePos).Trim();
}

Upvotes: 1

Related Questions