CodeImport.IsCodeType 属性

获取一个值,该值指示是否可以从 CodeImport 对象中获取 CodeType 对象。

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

语法

声明
ReadOnly Property IsCodeType As Boolean
bool IsCodeType { get; }
property bool IsCodeType {
    bool get ();
}
abstract IsCodeType : bool
function get IsCodeType () : boolean

属性值

类型:System.Boolean
一个布尔值,如果可以从 CodeImport 对象获取 CodeType 对象,该值为 true;否则为 false。

备注

如果为 true,则可使用查询接口或将该对象强制转换为 CodeType 对象。当 KindvsCMElementClassvsCMElementInterfacevsCMElementDelegatevsCMElementStructvsCMElementEnum 时,此值为 true。

说明说明

在进行某些类型的编辑之后,代码模型元素(如类、结构、函数、特性、委托等)的值可能是非确定性的,这意味着不能指望它们的值总是保持不变。有关更多信息,请参见 使用代码模型查找代码 (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
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);
    }
}

.NET Framework 安全性

请参见

参考

CodeImport 接口

IsCodeType 重载

EnvDTE80 命名空间

其他资源

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