CodeClass.RemoveBase 方法

从基列表中移除一个对象。

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

语法

声明
Sub RemoveBase ( _
    Element As Object _
)
void RemoveBase(
    Object Element
)
void RemoveBase(
    Object^ Element
)
abstract RemoveBase : 
        Element:Object -> unit 
function RemoveBase(
    Element : Object
)

参数

备注

必选。 一个 CodeElement,或它在集合中的名称。

移除的元素可以是集合中的某个 CodeElement 对象,也可以是集合中某唯一元素的名称。

提示

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

示例

 Sub RemoveBaseExample(ByVal dte As DTE2)

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

    ' 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 MsgBox("Remove all bases from " & cls.Name & "?", _
            MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            ' Remove every base except System.Object.
            Dim bases As New ArrayList()
            Dim base As CodeElement
            For Each base In cls.Bases
                If base.FullName <> ConvertFullName( _
                    cls.ProjectItem.ContainingProject.CodeModel, _
                    "System.Object") Then
                    bases.Add(base)
                End If
            Next

            For Each base In bases
                cls.RemoveBase(base)
            Next
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

Function ConvertFullName(ByVal cm As CodeModel, _
    ByVal fullName As String) As String

    ' Convert a .NET type name into a C++ type name.
    If (cm.Language = CodeModelLanguageConstants.vsCMLanguageVC) Or _
        (cm.Language = CodeModelLanguageConstants.vsCMLanguageMC) Then
        Return fullName.Replace(".", "::")
    Else
        Return fullName
    End If

End Function
public void RemoveBaseExample(DTE2 dte)
{
    // NOTE: This example requires a reference to the 
    //       System.Collections namespace.

    // 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 (MessageBox.Show("Remove all bases from " + cls.Name + "?", 
            "", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
            // Remove every base except System.Object.
            ArrayList bases = new ArrayList();
            
            foreach (CodeElement baseElement in cls.Bases)
            {
                if (baseElement.FullName != ConvertFullName(
                    cls.ProjectItem.ContainingProject.CodeModel, 
                    "System.Object"))
                    bases.Add(baseElement);
            }

            foreach (CodeElement baseElement in bases)
                cls.RemoveBase(baseElement);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

string ConvertFullName(CodeModel cm, string fullName)
{
    // Convert a .NET type name into a C++ type name.
    if ((cm.Language == CodeModelLanguageConstants.vsCMLanguageVC) || 
        (cm.Language == CodeModelLanguageConstants.vsCMLanguageMC))
        return fullName.Replace(".", "::");
    else
        return fullName;
}

.NET Framework 安全性

请参见

参考

CodeClass 接口

EnvDTE 命名空间

其他资源

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

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

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