次の方法で共有


方法 : Visual Basic プロジェクトの Imports プロパティを操作する

VSProject2 のほとんどのメソッドおよびプロパティは、Visual C# プロジェクトおよび Visual Basic プロジェクトに適用されます。詳細については、「方法: VSProject2 オブジェクトを使用して Visual Basic プロジェクトと C# プロジェクトを操作する」を参照してください。VSProject2 オブジェクトの Imports プロパティは、Visual Basic プロジェクト固有です。Imports コレクションを追加および列挙するためには、メソッドを使用して Imports オブジェクトにアクセスできるようにします。

次の手順では、Visual Studio アドインを使用して、Visual Basic プロジェクトの Imports プロパティをプログラムで制御する方法について説明します。

[!メモ]

実際に画面に表示されるダイアログ ボックスとメニュー コマンドは、アクティブな設定またはエディションによっては、ヘルプの説明と異なる場合があります。ここに記載されている手順は、全般的な開発設定が適用されているものとして記述されています。設定を変更するには、[ツール] メニューの [設定のインポートとエクスポート] をクリックします。詳細については、「Visual Studio の設定」を参照してください。

VSProject2 オブジェクトを使用して Visual Basic プロジェクトを制御するには

  1. Visual C# を使用して、Visual Studio アドイン プロジェクトを作成します。

  2. [プロジェクト] メニューの [参照の追加] をクリックし、[.NET] タブをクリックします。[VSLangProj]、[VSLangProj2]、および [VSLangProj80] を選択し、[OK] をクリックします。

  3. コンピューターに次のフォルダーを作成します。

    • <Installation Root>\UserFiles\MyProjects\MyTestProject

      この例では、<Installation Root> は "C:" になります。

  4. 次の using ステートメントを Connect.cs ファイルの先頭に追加します。

    using VSLangProj;
    using VSLangProj2;
    using VSLangProj80;
    using VSLangProj90;
    
  5. using VSLangProj100;次のメソッド呼び出しを OnConnection メソッドに追加します。

    public void OnConnection(object application, 
    ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        VBVSProj2Manip(_applicationObject);
    }
    
  6. CSVSProj2Manip メソッドの宣言を OnConnection メソッドの直後に追加します。

    public void CSVSProj2Manip(DTE2 dte)
    {
    }
    
  7. 次の宣言をメソッドの先頭に追加します。

    Solution2 soln = (Solution2)_applicationObject.Solution;
    String vbTemplatePath;
    String vbPrjPath;
    Project proj;
    VSProject2 vsproj;
    Imports impCollection;
    
  8. AddFromTemplate を使用して、Visual C# プロジェクトを作成します。

    • テンプレートを取得するための構文は EnvDTE80.Solution2.GetProjectTemplate("WindowsApplication.zip", "VisualBasic") です。この構文の "WindowsApplication.zip" という名前は、<Installation Root>\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\VisualBasic\Windows\1033 フォルダーにある WindowsApplication.zip ファイルから取得されています。Visual Studio のすべてのプロジェクトの種類に対応するこれらのファイルは <Installation Root>\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\Language フォルダーにあります。"VisualBasic" は、このプロジェクトが Visual Basic プロジェクトであることを示します。
    // Make sure you create the folders that 
    // make up the file path
    // on your computer. You can replace 
    // this with your own file path.
    vbPrjPath = "C:\\UserFiles\\MyProjects\\MyTestProject";
    // Get the project template path for a C# windows 
    // application.
    vbTemplatePath = soln.GetProjectTemplate
    ("WindowsApplication.zip", "VisualBasic");
            // Create a new Windows application by using the 
    // template obtained above.
    soln.AddFromTemplate(vbTemplatePath, vbPrjPath,
     "Test2VBProj", false);
    
  9. 次のコードを追加し、Imports プロパティを使用して取得した後の Imports の使用方法を示します。

    proj = soln.Projects.Item(1);
    // Get a reference to the VSProject2 object.
    vsproj = (VSProject2)proj.Object;
    // Add a reference to System.Security.dll.
    MessageBox.Show("Adding a reference to System.Security.dll");
    // Remove the <version number> in the following path
    // and replace it with one of the version 
    // number folders that appear 
    // in <installation root>\WINDOWS\Microsoft.NET\Framework
    // folder
    vsproj.References.Add
    ("C:\\WINDOWS\\Microsoft.NET\\Framework\\
    <version number>\\System.Security.dll");
    impCollection = vsproj.Imports;
    MessageBox.Show("The number of imports in this project is: " 
    + impCollection.Count.ToString() + "\n");
    MessageBox.Show
    ("Adding System.Security to the Imports collection.");
    impCollection.Add("System.Security");
    MessageBox.Show("The number of imports in this project is now: " 
    + impCollection.Count.ToString() + "\n");
    String temp = null;
    for (int i = 1; i <= impCollection.Count; i++)
    {
        temp = temp + impCollection.Item(i).ToString() + "\n";
    }
    MessageBox.Show("The Imports in this project are:" + "\n" + temp);
    

    VBVSProj2Manip メソッドは、VSProject2 オブジェクトを使用して次の操作を行います。

    • References を使用して、System.Security.dll への参照を追加します。

    • Imports プロパティを使用して、Imports へのハンドルを取得します。

    Imports のメソッドは、次の操作に使用します。

    • Add を使用して、System.Security を Imports コレクションに追加します。

    • Count プロパティを使用して、Imports コレクション内の項目数を表示します。

    • Item メソッドを使用して、Imports コレクション内の項目名を表示します。

    以下の使用例では、メソッド全体に try-catch ブロックを含めた完全なコードを示します。

  10. アドインをビルドするには、[ビルド] メニューの [ソリューションのビルド] をクリックします。

  11. Visual Studio 統合開発環境 (IDE: Integrated Development Environment) で、Visual Basic プロジェクトを開きます。

  12. [ツール] メニューの [アドイン マネージャー] をクリックし、[アドイン マネージャー] ダイアログ ボックスからアドインを選択します。[OK] をクリックしてアドインを実行します。

使用例

基本的な Visual Studio アドインの例を次に示します。この例には、Visual Studio オートメーションを通じて Imports プロパティを使用する方法が示されています。

using System;
using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using System.Windows.Forms;
using VSLangProj;
using VSLangProj2;
using VSLangProj80;
using VSLangProj90;
using VSLangProj100;
public void OnConnection(object application, 
ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    VBVSProj2Manip(_applicationObject);
}
public void VBVSProj2Manip(DTE2 dte)
{
    try
    {
        Solution2 soln = (Solution2)_applicationObject.Solution;
        String vbTemplatePath;
        String vbPrjPath;
        Project proj;
        VSProject2 vsproj;
        Imports impCollection;
        // Make sure you create the folders that make up the file path
        // on your computer. You can replace this with 
        // your own file path.
        vbPrjPath = "C:\\UserFiles\\MyProjects\\MyTestProject";
        // Get the project template path for a Visual Basic windows
        // application.
        vbTemplatePath = soln.GetProjectTemplate
("WindowsApplication.zip", "VisualBasic");
        // Create a new Windows application by using the 
        // template obtained above.
        soln.AddFromTemplate(vbTemplatePath, vbPrjPath,
 "Test2VBProj", false);
        proj = soln.Projects.Item(1);
        // Cast to the VSProject2 object.
        vsproj = (VSProject2)proj.Object;
        // Add a reference to System.Security.dll.
        MessageBox.Show("Adding a reference to System.Security.dll");
        // Remove the <version number> in the following path
        // and replace it with one of the version 
        // number folders that appear 
        // in <installation root>\WINDOWS\Microsoft.NET\Framework
        // folder
        vsproj.References.Add
("C:\\WINDOWS\\Microsoft.NET\\Framework\\
<version number>\\System.Security.dll");
        vsproj.Refresh();
        impCollection = vsproj.Imports;
        MessageBox.Show("The number of imports in this project is: " 
+ impCollection.Count.ToString() + "\n");
        MessageBox.Show("Adding System.Security to the 
Imports collection.");
        impCollection.Add("System.Security");
        MessageBox.Show("The number of imports in this project is now:
 " + impCollection.Count.ToString() + "\n");
        String temp = null;
        for (int i = 1; i <= impCollection.Count; i++)
        {
            temp = temp + impCollection.Item(i).ToString() + "\n";
        }
        MessageBox.Show("The Imports in this project are:" + "\n" 
+ temp);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80
Imports VSLangProj
Imports VSLangProj2
Imports VSLangProj80
Imports VSLangProj90
Imports VSLangProj100
Public Sub OnConnection(ByVal application As Object, _
 ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, _
 ByRef custom As Array) Implements IDTExtensibility2.OnConnection
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    VBVSProj2Manip(_applicationObject)
End Sub
Sub VBVSProj2Manip(ByVal dte As DTE2)
    Try
        Dim soln As Solution2 = CType(_applicationObject.Solution, _
        Solution2)
        Dim vbTemplatePath As String
        Dim vbPrjPath As String
        Dim proj As Project
        Dim vsproj As VSProject2
        Dim impCollection As [Imports]
        ' Create this or your own file path on your computer.
        ' The file path needs to exist before you run this add-in.
        vbPrjPath = "C:\UserFiles\MyProjects\MyTestProject"
        ' Get the project template path for a Visual Basic 
        ' Windows application.
        vbTemplatePath = soln.GetProjectTemplate _
         ("WindowsApplication.zip", "VisualBasic")
         ' Create a new Windows Application by using the 
        ' template obtained above.
        soln.AddFromTemplate(vbTemplatePath, vbPrjPath, _
        "Test2JSProj", False)
        proj = soln.Projects.Item(1)
        ' Cast the project to a VSProject2.
        vsproj = CType(proj.Object, VSProject2)
        ' Add a reference to System.Security.dll.
        MsgBox("Adding a reference to System.Security.dll")
        ' Remove the <version number> in the following path
        ' and replace it with one of the version 
        ' number folders that appear 
        ' in <installation root>\WINDOWS\Microsoft.NET\Framework
        ' folder
        vsproj.References.Add _
        ("C:\WINDOWS\Microsoft.NET\Framework\ _
        <version number>\System.Security.dll")
        impCollection = vsproj.Imports
        MsgBox("The number of imports in this project is: " & vbCr _
        & impCollection.Count.ToString())
        MsgBox("Adding System.Security to the Imports collection.")
        impCollection.Add("System.Security")
        MsgBox("The number of imports in this project is now: "  _
        & vbCr & impCollection.Count.ToString())
        Dim temp As String = ""
        For i As Integer = 1 To impCollection.Count
            temp = temp & impCollection.Item(i).ToString() & vbCr
        Next i
        MsgBox("The Imports in this project are:" & vbCr & temp)
    Catch ex As System.Exception
        MsgBox(ex.ToString)
    End Try
End Sub

コードのコンパイル

このコードをコンパイルするには、新しい Visual Studio アドイン プロジェクトを作成し、OnConnection メソッドのコードをこの例のコードで置き換えます。アドインの実行方法については、「方法: アドイン マネージャーを使用してアドインを制御する」を参照してください。

参照

概念

VSProject2 オブジェクトの概要

その他の技術情報

Visual Basic および Visual C# の各プロジェクトの拡張