CodeElement2 介面

代表原始程式檔中的程式碼項目或建構。

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

語法

'宣告
<GuidAttribute("F03DCCE8-233B-43D7-A66B-A66EFC1F85C3")> _
Public Interface CodeElement2 _
    Inherits CodeElement
[GuidAttribute("F03DCCE8-233B-43D7-A66B-A66EFC1F85C3")]
public interface CodeElement2 : CodeElement
[GuidAttribute(L"F03DCCE8-233B-43D7-A66B-A66EFC1F85C3")]
public interface class CodeElement2 : CodeElement
[<GuidAttribute("F03DCCE8-233B-43D7-A66B-A66EFC1F85C3")>]
type CodeElement2 =  
    interface
        interface CodeElement
    end
public interface CodeElement2 extends CodeElement

CodeElement2 型別會公開下列成員。

屬性

  名稱 說明
公用屬性 Children (繼承自 CodeElement)。
公用屬性 Children 取得物件的集合,此集合包含在 CodeElement2 中。
公用屬性 Collection (繼承自 CodeElement)。
公用屬性 Collection 取得 CodeElements 集合,此集合包含支援此屬性的 CodeElement2。
公用屬性 DTE (繼承自 CodeElement)。
公用屬性 DTE 取得最上層的擴充性物件。
公用屬性 ElementID 取得值,可用以唯一地辨識項目。尚未在 Visual C# 中進行實作。
公用屬性 EndPoint (繼承自 CodeElement)。
公用屬性 EndPoint 取得位於程式碼項目結尾的文字位置。
公用屬性 Extender[String] (繼承自 CodeElement)。
公用屬性 Extender[String] 傳回要求的擴充項 (如果適用於這個 CodeElement2 物件)。尚未在 Visual C# 中進行實作。
公用屬性 ExtenderCATID (繼承自 CodeElement)。
公用屬性 ExtenderCATID 取得 CodeElement2 物件的擴充項分類 ID (CATID)。尚未在 Visual C# 中進行實作。
公用屬性 ExtenderNames (繼承自 CodeElement)。
公用屬性 ExtenderNames 取得 CodeElement2 物件的可用擴充項清單。尚未在 Visual C# 中進行實作。
公用屬性 FullName (繼承自 CodeElement)。
公用屬性 FullName 取得 CodeElement2 物件檔案的完整路徑和名稱。
公用屬性 InfoLocation (繼承自 CodeElement)。
公用屬性 InfoLocation 取得程式碼模型的功能。
公用屬性 IsCodeType (繼承自 CodeElement)。
公用屬性 IsCodeType 取得是否可從 CodeElement2 物件取得的 CodeType 物件。
公用屬性 Kind (繼承自 CodeElement)。
公用屬性 Kind 取得列舉型別,用以定義程式碼項目的型別。
公用屬性 Language (繼承自 CodeElement)。
公用屬性 Language 取得程式語言,用以撰寫 CodeElement2。
公用屬性 Name (繼承自 CodeElement)。
公用屬性 Name 取得或設定 CodeElement2 物件的名稱。
公用屬性 ProjectItem (繼承自 CodeElement)。
公用屬性 ProjectItem 取得與 CodeElement 物件關聯的 ProjectItem 物件。
公用屬性 StartPoint (繼承自 CodeElement)。
公用屬性 StartPoint 取得 TextPoint 物件,此物件定義 CodeElement2 的開頭。

回頁首

方法

  名稱 說明
公用方法 GetEndPoint(vsCMPart) (繼承自 CodeElement)。
公用方法 GetEndPoint(vsCMPart) TextPoint 物件,用以標示程式碼項目定義的結尾。
公用方法 GetStartPoint(vsCMPart) (繼承自 CodeElement)。
公用方法 GetStartPoint(vsCMPart) 取得 TextPoint 物件,用以標示程式碼項目定義的開頭。
公用方法 RenameSymbol 變更已宣告的物件名稱,並且更新目前專案範圍內此物件的所有程式碼參考。

回頁首

備註

程式碼項目可以是任一個程式碼片段,不過一般而言,語言中的每個定義或宣告式語法都有一個 CodeElement2 物件。這表示檔案中大部分的最上層定義或宣告,或是類別定義中的任何語法形式等,都會有對應的 CodeElement2 物件。

注意事項注意事項

在特定類型的編輯之後,程式碼模型項目 (例如類別、結構、函式、屬性、委派等) 的值可能不具決定性,表示其值不一定維持相同。如需詳細資訊,請參閱使用程式碼模型探索程式碼 (Visual Basic) 的<程式碼模型項目值可以變更>一節。

範例

[Visual Basic]

Sub IsCodeTypeExample(ByVal dte As DTE2)

    ' NOTE: This example requires a reference to the System.Text 
    '       namespace.

    ' Before running this example, open a code document from a project.
    Dim item As ProjectItem = dte.ActiveDocument.ProjectItem
    Dim sb As New StringBuilder

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

    MsgBox(item.Name & " contains the following elements:" & vbCrLf & _
        vbCrLf & sb.ToString())

End Sub

Sub RecurseElements(ByVal elems As CodeElements, _
    ByVal level As Integer, ByVal sb As StringBuilder)

    Dim elem As CodeElement
    For Each elem In elems
        ' Add element to the list of names.
        sb.Append(" "c, level * 8)
        sb.Append(elem.Name & " [" & elem.Kind.ToString() & "]" & _
            vbCrLf)

        ' Call this function recursively if element has children.
        If elem.Kind = vsCMElement.vsCMElementNamespace Then
            RecurseElements(CType(elem, CodeNamespace).Members, _
                level + 1, sb)
        ElseIf elem.IsCodeType Then
            RecurseElements(CType(elem, CodeType).Members, _
                level + 1, sb)
        End If
    Next
End Sub

[C#]

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);
    }
}

請參閱

參考

EnvDTE80 命名空間

其他資源

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

使用程式碼模型探索程式碼 (Visual Basic)

使用程式碼模型探索程式碼 (Visual C#)