Freigeben über


FileCodeModel.CodeElementFromPoint-Methode

Gibt ein Codeelement an einer bestimmten Position in einer Quelldatei zurück.

Namespace:  EnvDTE
Assembly:  EnvDTE (in EnvDTE.dll)

Syntax

'Declaration
Function CodeElementFromPoint ( _
    Point As TextPoint, _
    Scope As vsCMElement _
) As CodeElement
CodeElement CodeElementFromPoint(
    TextPoint Point,
    vsCMElement Scope
)
CodeElement^ CodeElementFromPoint(
    TextPoint^ Point, 
    vsCMElement Scope
)
abstract CodeElementFromPoint : 
        Point:TextPoint * 
        Scope:vsCMElement -> CodeElement 
function CodeElementFromPoint(
    Point : TextPoint, 
    Scope : vsCMElement
) : CodeElement

Parameter

  • Point
    Typ: EnvDTE.TextPoint
    Erforderlich.Ein TextPoint-Objekt, das die Editor-Position darstellt, für die ein Codeelement abgerufen werden soll.
  • Scope
    Typ: EnvDTE.vsCMElement
    Erforderlich.Ein vsCMElement-Wert, der das Codeelement des angegebenen Typs darstellt, das die Editor-Position enthält.

Rückgabewert

Typ: EnvDTE.CodeElement
Ein CodeElement-Objekt.

Hinweise

CodeElementFromPoint gibt das dem TextPoint zugeordnete Codeelement entsprechend dem angegebenen Gültigkeitsbereich bzw. der Granularität der Kapselung zurück. Wenn die Editor-Position in keinem Codeelement des angegebenen Typs enthalten ist, schlägt diese Methode fehl.

Beispiele

Sub CodeElementFromPointExample(ByVal dte As DTE2)

    ' Before running this example, open a code document from a project
    ' and place the insertion point anywhere inside the source code.
    Try
        Dim sel As TextSelection = _
            CType(dte.ActiveDocument.Selection, TextSelection)
        Dim pnt As TextPoint = CType(sel.ActivePoint, TextPoint)

        ' Discover every code element containing the insertion point.
        Dim fcm As FileCodeModel = _
            dte.ActiveDocument.ProjectItem.FileCodeModel
        Dim elems As String
        Dim elem As CodeElement
        Dim scope As vsCMElement
        For Each scope In [Enum].GetValues(scope.GetType())
            elem = fcm.CodeElementFromPoint(pnt, scope)
            If IsNothing(elem) = False Then
                elems &= elem.Name & " (" & scope.ToString() & ")" & _
                    vbCrLf
            End If
        Next

        MsgBox("The following elements contain the insertion point:" _
            & vbCrLf & vbCrLf & elems)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub
public void CodeElementFromPointExample(DTE2 dte)
{
    // Before running this example, open a code document from a project
    // and place the insertion point anywhere inside the source code.
    try
    {
        TextSelection sel = 
            (TextSelection)dte.ActiveDocument.Selection;
        TextPoint pnt = (TextPoint)sel.ActivePoint;

        // Discover every code element containing the insertion point.
        FileCodeModel fcm = 
            dte.ActiveDocument.ProjectItem.FileCodeModel;
        string elems = "";
        vsCMElement scopes = 0;

        foreach (vsCMElement scope in Enum.GetValues(scopes.GetType()))
        {
            CodeElement elem = fcm.CodeElementFromPoint(pnt, scope);

            if (elem != null)
                elems += elem.Name + " (" + scope.ToString() + ")\n";
        }

        MessageBox.Show(
            "The following elements contain the insertion point:\n\n" + 
            elems);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

.NET Framework-Sicherheit

Siehe auch

Referenz

FileCodeModel Schnittstelle

EnvDTE-Namespace

Weitere Ressourcen

Gewusst wie: Kompilieren und Ausführen der Codebeispiele für das Automatisierungsobjektmodell