如何:编译和运行自动化对象模型代码示例

在 Visual Studio 2013 中,弃用外接程序。 我们建议您将外接程序升级到 VSPackage 扩展。 有关如何升级的更多信息,请参阅 常见问题:将外接程序转换为 VSPackage 扩展

在自动化对象模型参考主题中找到的代码示例设计为在用**“外接程序向导”**创建的外接程序中运行。 以下几节介绍了如何编译和运行自动化对象模型代码示例。

在外接程序中运行代码示例

  1. 使用**“外接程序向导”**创建外接程序。

    有关详细信息,请参阅 如何:创建外接程序

  2. 将代码示例添加至外接程序的 Connect 类。

  3. 从外接程序的 OnConnection 方法中调用代码示例过程。 如果代码示例要求 DTE2 对象作为变量,请传入在外接程序代码中声明的 _applicationObject 成员变量。 下面的代码显示了 Visual Basic 和 Visual C# 外接程序中 Connect 类的相关部分:

    Public Sub OnConnection(ByVal application As Object, ... ) _
      Implements IDTExtensibility2.OnConnection
    
        _applicationObject = CType(application, EnvDTE80.DTE2)
            .
            .
            .
        ' Pass the _applicationObject member variable to the code 
        ' example.
        MyCodeExample(_applicationObject)
    End Sub
    ' Other methods in the Connect class.
        .
        .
        .
    
    ' This is the code example that was added to the Connect class.
    Sub MyCodeExample(ByVal dte As DTE2)
        .
        .
        .
    End Sub
    
    public void OnConnection(object application, ... , ref Array custom)
    {
        _applicationObject = (DTE2)application;
            .
            .
            .
        // Pass the _applicationObject member variable to the code 
        // example.
        MyCodeExample(_applicationObject);
    }
    
    
    // Other methods in the Connect class.
        .
        .
        .
    
    // This is the code example that was added to the Connect class.
    void MyCodeExample(DTE2 dte)
    {
        .
        .
        .
    }
    
  4. 对于 Visual C# 外接程序,可以通过以下方式添加对 System.Windows.Forms 程序集的引用:打开“解决方案资源管理器”中外接程序项目的快捷菜单,选择“添加”、“引用”,选择“添加新引用”,然后在“添加引用”对话框中选择 System.Windows.Forms.dll。

    1. 对于 Visual C# 外接程序,在 Connect.cs 文件的顶部插入下面的代码:

      using System.Windows.Forms;
      
  5. 通过从**“生成”菜单中选择“生成解决方案”**来编译外接程序。

  6. 从**“工具”菜单中选择“外接程序管理器”**。

    出现**“外接程序管理器”**对话框。

  7. 在**“外接程序管理器”对话框中,选择外接程序名称旁边的复选框以加载此外接程序,然后单击“确定”**。

请参见

任务

如何:创建外接程序

如何:使用外接程序管理器控制外接程序

概念

外接程序注册