CodeClass.Namespace 属性

获取一个定义父命名空间的对象。

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

语法

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

属性值

类型:EnvDTE.CodeNamespace
一个 CodeNamespace 对象。

备注

返回与此 CodeType 关联的 CodeNamespace 对象。 对于在文件顶级声明的类型或者对于默认命名空间或全局命名空间,Namespace 返回 Nothing。

备注

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

示例

 Sub NamespaceExample(ByVal dte As DTE2)

    ' Before running this example, open a code document from a project
    ' and place the insertion point inside a class definition.
    Try
        ' Retrieve the CodeClass at the insertion point.
        Dim sel As TextSelection = _
            CType(dte.ActiveDocument.Selection, TextSelection)
        Dim cls As CodeClass = _
            CType(sel.ActivePoint.CodeElement( _
            vsCMElement.vsCMElementClass), CodeClass)

        If IsNothing(cls.Namespace) = False Then
            MsgBox(cls.Name & " belongs to the " & _
                cls.Namespace.Name & " namespace.")
        Else
            MsgBox(cls.Name & " doesn't belong to a namespace.")
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub
public void NamespaceExample(DTE2 dte)
{
    // Before running this example, open a code document from a project
    // and place the insertion point inside a class definition.
    try
    {
        // Retrieve the CodeClass at the insertion point.
        TextSelection sel = 
            (TextSelection)dte.ActiveDocument.Selection;
        CodeClass cls = 
            (CodeClass)sel.ActivePoint.get_CodeElement(
            vsCMElement.vsCMElementClass);

        if (cls.Namespace != null)
            MessageBox.Show(cls.Name + " belongs to the " + 
                cls.Namespace.Name + " namespace.");
        else
            MessageBox.Show(cls.Name + 
                " doesn't belong to a namespace.");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

.NET Framework 安全性

请参阅

参考

CodeClass 接口

EnvDTE 命名空间

其他资源

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

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

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