共用方式為


CodeModel2.AddDelegate 方法

建立新的委派程式碼建構,並在正確位置插入程式碼。

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

語法

'宣告
Function AddDelegate ( _
    Name As String, _
    Location As Object, _
    Type As Object, _
    Position As Object, _
    Access As vsCMAccess _
) As CodeDelegate
CodeDelegate AddDelegate(
    string Name,
    Object Location,
    Object Type,
    Object Position,
    vsCMAccess Access
)
CodeDelegate^ AddDelegate(
    String^ Name, 
    Object^ Location, 
    Object^ Type, 
    Object^ Position, 
    vsCMAccess Access
)
abstract AddDelegate : 
        Name:string * 
        Location:Object * 
        Type:Object * 
        Position:Object * 
        Access:vsCMAccess -> CodeDelegate
function AddDelegate(
    Name : String, 
    Location : Object, 
    Type : Object, 
    Position : Object, 
    Access : vsCMAccess
) : CodeDelegate

參數

  • Name
    類型:String

    必要項。 要加入的委派名稱。

  • Location
    類型:Object

    必要項。 新委派定義的路徑和檔名。 根據程式語言的不同,檔名可能與專案檔相關或完全一致。 如果這個檔案目前不是專案項目,將會將它加入到專案中。 如果無法建立這個檔案並加入至專案,則 AddDelegate 將會失敗。

  • Type
    類型:Object

    必要項。 vsCMTypeRef 常數,表示函式所傳回的資料型別。 這個值可以是 CodeTypeRef 物件、vsCMTypeRef 常數,或完整型別名稱。

  • Position
    類型:Object

    選擇項。 預設値 = 0。 其後要加入新項目的程式碼項目。 如果這個值是 CodeElement,則會緊接在其後加入新項目。

    如果這個值是長資料型別,那麼 AddDelegate 會指示其後要加入新項目的項目。

    由於集合是從 1 開始計算,傳遞 0 表示新的項目必須放在集合起始的位置。 值為 -1 時表示這個項目必須放在集合結尾的位置。

  • Access
    類型:vsCMAccess

    選擇項。 vsCMAccess 常數。

傳回值

類型:CodeDelegate
CodeDelegate 物件。

備註

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

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

注意事項注意事項

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

範例

Sub AddDelegateExample2(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

        ' Create a new delegate.
        cm.AddDelegate("TestDelegate", projItem.Name, _
            vsCMTypeRef.vsCMTypeRefInt)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub
public void AddDelegateExample2(DTE2 dte)
{
    // Before running this example, open a code document from 
    // a project.
    try
    {
        ProjectItem projItem = dte.ActiveDocument.ProjectItem;
        CodeModel cm = projItem.ContainingProject.CodeModel;

        // Create a new delegate.
        cm.AddDelegate("TestDelegate", projItem.Name, 
            vsCMTypeRef.vsCMTypeRefInt, -1, 
            vsCMAccess.vsCMAccessPublic);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

.NET Framework 安全性

請參閱

參考

CodeModel2 介面

EnvDTE80 命名空間

其他資源

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

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

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