CodeClass2.Children 属性

获取此代码类中包含的对象的集合。

命名空间:  EnvDTE80
程序集:  EnvDTE80(在 EnvDTE80.dll 中)

语法

声明
ReadOnly Property Children As CodeElements
CodeElements Children { get; }
property CodeElements^ Children {
    CodeElements^ get ();
}
abstract Children : CodeElements with get
function get Children () : CodeElements

属性值

类型:CodeElements
一个 CodeElements 集合。

备注

如果代码类没有子级,则返回 Nothing 或 nullnull 引用(在 Visual Basic 中为 Nothing)。

此属性主要由 Visual C++ 使用。 Children 返回可从代码元素返回的每个对象。 例如,类返回成员、基、实现的接口、特性、注释等。

若要循环访问命名空间或类型(类、结构、接口等)的成员,必须使用查询接口 (QI) 或将 CodeElement 强制转换为 CodeNamespace,然后使用 Members 属性。

Children 返回可通过此代码类引用的所有相关 CodeElement 对象的集合。 例如,这可能包括类的元数据代码元素(可能还有 Visual C++ declspec),以及基于 Visual C++ 中的特性化编程功能的外供代码、模板参数等。

Children 属性可能返回 Nothing,这取决于具体的对象和语言。 Visual Studio 中无需支持此行为。

备注

在进行某些类型的编辑之后,代码模型元素(如类、结构、函数、特性、委托等)的值可能是非确定性的,这意味着不能指望它们的值总是保持不变。有关更多信息,请参见 使用代码模型查找代码 (Visual Basic) 中的“代码模型元素的值可能会更改”一节。

示例

[Visual Basic]

Sub ChildrenExample(ByVal dte As DTE2)
    ' Before running this example, open a code document from a project
    ' and place the insertion point inside a namespace definition.
    Try
        ' Retrieve the CodeNamespace at the insertion point.
        Dim sel As TextSelection = _
            CType(dte.ActiveDocument.Selection, TextSelection)
        Dim spc As CodeNamespace = _
            CType(sel.ActivePoint.CodeElement( _
            vsCMElement.vsCMElementNamespace), CodeNamespace)

        ' Find the namespace's children.
        Dim children As String
        Dim elem As CodeElement
        For Each elem In spc.Children
            children &= elem.Name & vbCrLf
        Next

        MsgBox(spc.Name & " has the following child code elements:" & _
            vbCrLf & vbCrLf & children)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

[C#]

public void ChildrenExample(DTE2 dte)
{
    // Before running this example, open a code document from a project
    // and place the insertion point inside a namespace definition.
    try
    {
        // Retrieve the CodeNamespace at the insertion point.
        TextSelection sel = 
            (TextSelection)dte.ActiveDocument.Selection;
        CodeNamespace spc = 
            (CodeNamespace)sel.ActivePoint.get_CodeElement(
            vsCMElement.vsCMElementNamespace);

        // Find the namespace's children.
        string children = "";
        
        foreach (CodeElement elem in spc.Children)
        children += elem.Name + "\r\n";

        MessageBox.Show(spc.Name + 
            " has the following child code elements:" + "\r\n\r\n" + 
            children);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

.NET Framework 安全性

请参阅

参考

CodeClass2 接口

EnvDTE80 命名空间

其他资源

如何:编译和运行自动化对象模型代码示例

使用代码模型查找代码 (Visual Basic)

使用代码模型查找代码 (Visual C#)