Share via


CodeClass.AddImplementedInterface 方法

將介面加入至繼承之物件的清單中。

命名空間:  EnvDTE
組件:  EnvDTE (在 EnvDTE.dll 中)

語法

'宣告
Function AddImplementedInterface ( _
    Base As Object, _
    Position As Object _
) As CodeInterface
CodeInterface AddImplementedInterface(
    Object Base,
    Object Position
)
CodeInterface^ AddImplementedInterface(
    Object^ Base, 
    Object^ Position
)
abstract AddImplementedInterface : 
        Base:Object * 
        Position:Object -> CodeInterface 
function AddImplementedInterface(
    Base : Object, 
    Position : Object
) : CodeInterface

參數

  • Position
    型別:System.Object
    選擇項。預設 = 0。其後要加入新項目的程式碼項目。如果這個值是 CodeElement,則會緊接在其後加入新項目。
    如果這個值是 Long,那麼 AddImplementedInterface 會指出要在後面加入新項目的項目。
    由於集合是從 1 開始計算,傳遞 0 表示新的項目必須放在集合起始的位置。值為 -1 時表示這個項目必須放在集合結尾的位置。

傳回值

型別:EnvDTE.CodeInterface
CodeInterface 物件。

備註

AddImplementedInterface 會加入 CodeClass 所實作介面的參考。 AddImplementedInterface 不會插入介面成員的方法 Stub。

Visual C++ 的完整型別名稱必須使用冒號 (::) 分隔。 其他所有語言都支援句號分隔的格式。

引數的正確性取決於程式碼模型後的程式語言。

注意事項注意事項

在特定類型的編輯之後,程式碼模型項目 (例如類別、結構、函式、屬性、委派等) 的值可能不具決定性,表示其值不一定維持相同。 如需詳細資訊,請參閱使用程式碼模型探索程式碼 (Visual Basic) 的<程式碼模型項目值可以變更>一節。

範例

 Sub AddImplementedInterfaceExample(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)
        Dim cm As CodeModel = _
            cls.ProjectItem.ContainingProject.CodeModel

        ' Add a new implemented interface to the class.
        cls.AddImplementedInterface(ConvertFullName(cm, _
            "System.IDisposable"))
    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 AddImplementedInterfaceExample(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);
        CodeModel cm = cls.ProjectItem.ContainingProject.CodeModel;

        // Add a new implemented interface to the class.
        cls.AddImplementedInterface(ConvertFullName(cm, 
            "System.IDisposable"), -1);
    }
    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 命名空間

其他資源

HOW TO:編譯和執行 Automation 物件模型程式碼範例

使用程式碼模型探索程式碼 (Visual Basic)

使用程式碼模型探索程式碼 (Visual C#)