Export (0) Print
Expand All

CodeElement2 Interface 

Represents a code element or construct in a source file.

Namespace: EnvDTE80
Assembly: EnvDTE80 (in envdte80.dll)

[GuidAttribute("F03DCCE8-233B-43D7-A66B-A66EFC1F85C3")] 
public interface CodeElement2 : CodeElement
/** @attribute GuidAttribute("F03DCCE8-233B-43D7-A66B-A66EFC1F85C3") */ 
public interface CodeElement2 extends CodeElement
GuidAttribute("F03DCCE8-233B-43D7-A66B-A66EFC1F85C3") 
public interface CodeElement2 extends CodeElement

A code element can be any fragment of code, but generally, there is a CodeElement2 object for each definition or declarative syntax in a language. This means that for most top-level definitions or declarations in a file, or for any syntactic form in a class definition, and so forth, there is a corresponding CodeElement2 object.

NoteNote

The values of code model elements such as classes, structs, functions, attributes, delegates, and so forth can be non-deterministic after making certain kinds of edits, meaning that their values cannot be relied upon to always remain the same. For more information, see the section Code Model Element Values Can Change in Discovering Code with the Code Model (Visual Basic).

public void IsCodeTypeExample(DTE2 dte)
{
    // NOTE: This example requires a reference to the System.Text 
    //       namespace.

    // Before running this example, open a code document from a 
    // project.
    ProjectItem item = dte.ActiveDocument.ProjectItem;
    StringBuilder sb = new StringBuilder();

    RecurseElements(item.FileCodeModel.CodeElements, 0, sb);

    MessageBox.Show(item.Name + " contains the following elements:" + 
        Environment.NewLine + Environment.NewLine + sb.ToString());
}

void RecurseElements(CodeElements elems, int level, StringBuilder sb)
{
    foreach (CodeElement elem in elems)
    {
        // Add element to the list of names.
        sb.Append(' ', level * 8);
        sb.Append(elem.Name + " [" + elem.Kind.ToString() + "]" + 
            Environment.NewLine);

        // Call this function recursively if element has children.
        if (elem.Kind == vsCMElement.vsCMElementNamespace)
            RecurseElements(((CodeNamespace)elem).Members, 
                level + 1, sb);
        else if (elem.IsCodeType)
            RecurseElements(((CodeType)elem).Members, level + 1, sb);
    }
}
Show:
© 2015 Microsoft