共用方式為


FileCodeModel2.AddInterface 方法 (String, Object, Object, vsCMAccess)

建立新的介面程式碼建構並將程式碼插入正確位置。

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

語法

'宣告
Function AddInterface ( _
    Name As String, _
    Position As Object, _
    Bases As Object, _
    Access As vsCMAccess _
) As CodeInterface
CodeInterface AddInterface(
    string Name,
    Object Position,
    Object Bases,
    vsCMAccess Access
)
CodeInterface^ AddInterface(
    String^ Name, 
    Object^ Position, 
    Object^ Bases, 
    vsCMAccess Access
)
abstract AddInterface : 
        Name:string * 
        Position:Object * 
        Bases:Object * 
        Access:vsCMAccess -> CodeInterface 
function AddInterface(
    Name : String, 
    Position : Object, 
    Bases : Object, 
    Access : vsCMAccess
) : CodeInterface

參數

  • Position
    型別:System.Object
    選擇項。預設値 = 0。其後要加入新項目的程式碼項目。如果這個值是 CodeElement,則會緊接在其後加入新項目。
    如果這個值是長資料型別,那麼 AddInterface 會指示其後要加入新項目的項目。
    由於集合是從 1 開始計算,傳遞 0 表示新的項目必須放在集合起始的位置。值為 -1 時表示這個項目必須放在集合結尾的位置。
  • Bases
    型別:System.Object
    選擇項。預設值為 Nothing。Variant,可存放具有完整型別名稱或 CodeInterface 物件 (新介面會從其衍生) 的 SafeArray。

傳回值

型別:EnvDTE.CodeInterface
CodeInterface 物件。

備註

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

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

範例

Sub AddInterfaceExample2(ByVal dte As DTE2)

    ' Before running this example, open a code document from a project.
    Try
        Dim projItem As ProjectItem = dte.ActiveDocument.ProjectItem
        Dim cm As CodeModel = projItem.ContainingProject.CodeModel

        ' Initialize the base interfaces array.
        Dim bases() As Object = { _
            ConvertFullName(cm, "System.IDisposable"), _
            ConvertFullName(cm, "System.IComparable") _
       }

        ' Create a new class.
        projItem.FileCodeModel.AddInterface("TestInterface", , bases)
    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 AddInterfaceExample2(DTE2 dte)
{
    // Before running this example, open a code document from 
    // a project.
    try
    {
        ProjectItem projItem = dte.ActiveDocument.ProjectItem;
        CodeModel cm = projItem.ContainingProject.CodeModel;

        // Initialize the base interfaces array.
        object[] bases = {
        ConvertFullName(cm, "System.IDisposable"), 
        ConvertFullName(cm, "System.IComparable")
        };

        // Create a new class.
        projItem.FileCodeModel.AddInterface("TestInterface", -1, bases, 
            vsCMAccess.vsCMAccessPublic);
    }
    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 安全性

請參閱

參考

FileCodeModel2 介面

AddInterface 多載

EnvDTE80 命名空間

其他資源

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