创建新的委托代码构造,并将代码插入正确的位置。
命名空间:  EnvDTE80
程序集:  EnvDTE80(在 EnvDTE80.dll 中)
语法
声明
Function AddDelegate ( _
    Name As String, _
    Type As Object, _
    Position As Object, _
    Access As vsCMAccess _
) As CodeDelegate
CodeDelegate AddDelegate(
    string Name,
    Object Type,
    Object Position,
    vsCMAccess Access
)
CodeDelegate^ AddDelegate(
    String^ Name, 
    Object^ Type, 
    Object^ Position, 
    vsCMAccess Access
)
abstract AddDelegate : 
        Name:string * 
        Type:Object * 
        Position:Object * 
        Access:vsCMAccess -> CodeDelegate 
function AddDelegate(
    Name : String, 
    Type : Object, 
    Position : Object, 
    Access : vsCMAccess
) : CodeDelegate
参数
- Name
 类型:System.String
 必选。要添加的委托的名称。
- Type
 类型:System.Object
 必选。一个 vsCMTypeRef 常数,该常数指示函数返回的数据类型。它可以是 CodeTypeRef 对象、vsCMTypeRef 常数或完全限定的类型名。
- Position
 类型:System.Object
 可选。默认值 = 0。将在其后添加新元素的代码元素。如果该值为 CodeElement,则紧跟在其后添加新元素。
 如果该值为 Long 数据类型,则 AddDelegate 指示在哪个元素后添加新元素。
 因为集合从 1 开始计数,所以传递 0 指示应将新元素放置在集合的开始处。值为 -1 表示应将元素放在结尾处。
- Access
 类型:EnvDTE.vsCMAccess
 可选。一个 vsCMAccess 常数。
返回值
类型:EnvDTE.CodeDelegate
一个 CodeDelegate 对象。
实现
FileCodeModel.AddDelegate(String, Object, Object, vsCMAccess)
备注
Visual C++ 要求其完全限定的类型名使用以冒号分隔 (::) 的格式。 所有其他语言都支持以句点分隔的格式。
参数正确与否由代码模型后面的语言决定。
示例
Sub AddDelegateExample(ByVal dte As DTE2)
    ' Before running this example, open a code document from a project.
    Try
        Dim projItem As ProjectItem = dte.ActiveDocument.ProjectItem
        ' Create a new delegate.
        projItem.FileCodeModel.AddDelegate("TestDelegate", _
            vsCMTypeRef.vsCMTypeRefInt)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub
public void AddDelegateExample(DTE2 dte)
{
    // Before running this example, open a code document from 
    // a project.
    try
    {
        ProjectItem projItem = dte.ActiveDocument.ProjectItem;
        // Create a new delegate.
        projItem.FileCodeModel.AddDelegate("TestDelegate", 
            vsCMTypeRef.vsCMTypeRefInt, -1, 
            vsCMAccess.vsCMAccessPublic);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
.NET Framework 安全性
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。