Get function body programatically using Automation RRS feed

  • Question

  • Hi,

    I am integrating VisualStudio and trapped the mouse cursor on the code file to get selection type (function )using



    sel = (TextSelection)dte.ActiveDocument.Selection;



    pnt = (TextPoint)sel.ActivePoint;


    FileCodeModel fcm = dte.ActiveDocument.ProjectItem.FileCodeModel;

    codeElement = fcm.CodeElementFromPoint(pnt, elemType);

    codeFunction = codeElement

    as CodeFunction;

    and able to the selected function prototype/signature using the following.

    vsCMElement elemType = vsCMElement.vsCMElementFunction;



    methodSignature = codeFunction.get_Prototype((int)EnvDTE.vsCMPrototype.vsCMPrototypeFullname | (int)EnvDTE.vsCMPrototype.vsCMPrototypeType | (int)EnvDTE.vsCMPrototype.vsCMPrototypeParamNames | (int)EnvDTE.vsCMPrototype.vsCMPrototypeParamTypes);


    I want to get the body of the function but I am not able to find any switch in EnvDTE.vsCMPrototype which returns the body of the function .


    Can any body help



    Tuesday, March 23, 2010 11:53 AM



    The automation model (EnvDTE, FileCodeModel) doesn't support getting the statements inside the body of methods. So, you have to get the code by yourself and parse it. The CodeElement has GetStartPoint / GetEndPoint methods that can receive the EnvDTE.vsCMPart.vsCMPartBody value.
    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/
    • Proposed as answer by Carlos J. Quintero Tuesday, March 23, 2010 4:24 PM
    • Marked as answer by Chao Kuo Tuesday, March 30, 2010 1:54 AM
    Tuesday, March 23, 2010 4:24 PM