次の方法で共有


CodeClass2.RemoveBase メソッド

ベースの一覧からオブジェクトを削除します。

名前空間:  EnvDTE80
アセンブリ:  EnvDTE80 (EnvDTE80.dll 内)

構文

'宣言
Sub RemoveBase ( _
    Element As Object _
)
void RemoveBase(
    Object Element
)
void RemoveBase(
    Object^ Element
)
abstract RemoveBase : 
        Element:Object -> unit
function RemoveBase(
    Element : Object
)

パラメーター

  • Element
    型 : Object

    必須。 コレクション内の CodeElement またはコレクション内の要素の名前。

解説

削除する要素には、コレクション内の CodeElement オブジェクトを指定することも、コレクション内の一意の要素の名前を指定することもできます。

注意

特定の種類の編集を行った後に、クラス、構造体、関数、属性、デリゲートなどのコード モデル要素の値を決定できないことがあります。つまり、これらの要素の値は、信頼できる値ではなく、常に同じ値になるとは限りません。詳細については、「コード モデルを使用したコードの調査 (Visual Basic)」で、コード モデル要素値を変更する方法についての説明を参照してください。

[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

[C#]

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 セキュリティ

  • 直前の呼び出し元に対する完全な信頼。このメンバーは、部分的に信頼されているコードから使用することはできません。詳細については、「部分信頼コードからのライブラリの使用」を参照してください。

参照

関連項目

CodeClass2 インターフェイス

EnvDTE80 名前空間

その他の技術情報

方法 : オートメーション オブジェクト モデルのコード例をコンパイルおよび実行する

コード モデルを使用したコードの調査 (Visual Basic)

コード モデルを使用したコードの調査 (Visual C#)