共用方式為


EditPoint2.CodeElement 屬性 (vsCMElement)

取得在 EditPoint 位置的程式碼項目。

命名空間:  EnvDTE80
組件:  EnvDTE80 (在 EnvDTE80.dll 中)

語法

'宣告
ReadOnly Property CodeElement ( _
    Scope As vsCMElement _
) As CodeElement
CodeElement this[
    vsCMElement Scope
] { get; }
property CodeElement^ CodeElement[[InAttribute] vsCMElement Scope] {
    CodeElement^ get ([InAttribute] vsCMElement Scope);
}
abstract CodeElement : CodeElement
JScript 不支援索引屬性。

參數

屬性值

型別:EnvDTE.CodeElement
CodeElement 物件。

備註

如果指定的程式碼項目型別不在 EditPoint 位置,則 CodeElement 會傳回 Nothing。 CodeElement 是 TextPoint.Parent.Parent.ProjectItem.CodeModel.CodeElementFromPoint(TextPoint, <scope>) 的捷徑

範例

Sub CodeElementExample(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 elems As String
        Dim elem As CodeElement
        Dim scope As vsCMElement
        For Each scope In [Enum].GetValues(scope.GetType())
            elem = pnt.CodeElement(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 CodeElementExample(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.
        string elems = "";
        vsCMElement scopes = 0;

        foreach (vsCMElement scope in Enum.GetValues(scopes.GetType()))
        {
            CodeElement elem = pnt.CodeElement(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 安全性

請參閱

參考

EditPoint2 介面

CodeElement 多載

EnvDTE80 命名空間

其他資源

HOW TO:編譯和執行 Automation 物件模型程式碼範例