Aracılığıyla paylaş


Nasıl yapılır: Visual Basic Projelerinin Imports Özelliğini İşleme

Çoğu VSProject2 yöntemleri ve özellikleri, Visual C# ve Visual Basic projelerine uygulanır.Daha fazla bilgi için bkz. Nasıl yapılır: VSProject2 Nesnesini Kullanarak Visual Basic ve C# Projelerini İşleme.VSProject2 nesnesinin Imports özelliği Visual Basic projelerine özgüdür.Imports koleksiyonu ekleme ve numaralandırma için yöntemlerle Imports nesnesine erişim sağlar.

Aşağıdaki adımlar, bir Visual Studio eklentisi kullanarak bir Visual Basic projesinde Imports özelliğinin program aracılığıyla nasıl denetleneceğini ayrıntılı olarak anlatmaktadır.

[!NOT]

Bilgisayarınız, aşağıdaki yönergelerde yer alan Visual Studio kullanıcı arabirimi öğelerinden bazıları için farklı adlar veya konumlar gösterebilir.Sahip olduğunuz Visual Studio sürümü ve kullandığınız ayarlar bu öğeleri belirler.Daha fazla bilgi için bkz. Visual Studio'da Geliştirme Ayarlarını özelleştirme.

Visual Basic projelerini denetleyecek VSProject2 nesnesini kullanmak için

  1. Visual C# kullanarak Visual Studio eklenti projesi oluşturun.

  2. Proje menüsünde, Başvuru Ekle'ye tıklayın, .NET sekmesine tıklayın, VSLangProj, VSLangProj2 ve VSLangProj80 öğesini seçin ve sonra Tamama tıklayın.

  3. Bilgisayarınızda bir klasör oluşturun:

    • <Installation Root>\UserFiles\MyProjects\MyTestProject

      Bu örnekte, <Installation Root> öğesi "C"dir.

  4. Connects.ts dosyasının en üstüne aşağıdaki kullanım açıklamalarını ekleyin.

    using VSLangProj;
    using VSLangProj2;
    using VSLangProj80;
    using VSLangProj90;
    
  5. VSLangProj100 kullanma;Aşağıdaki yöntem çağrısını OnConnection yöntemine ekleyin.

    public void OnConnection(object application, 
    ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        VBVSProj2Manip(_applicationObject);
    }
    
  6. CSVSProj2Manip yöntemi bildirimini doğrudan OnConnection yöntemi altına ekleyin.

    public void CSVSProj2Manip(DTE2 dte)
    {
    }
    
  7. Aşağıdaki bildirimleri yöntemin en üstüne ekleyin.

    Solution2 soln = (Solution2)_applicationObject.Solution;
    String vbTemplatePath;
    String vbPrjPath;
    Project proj;
    VSProject2 vsproj;
    Imports impCollection;
    
  8. Bir proje oluşturmak için AddFromTemplate to create a Visual C# kullanın.

    • Şablonları almak için kullanılan sözdizimi şudur: EnvDTE80.Solution2.GetProjectTemplate("WindowsApplication.zip", "VisualBasic"); burada "WindowsApplication.zip" adı <Installation Root>\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\VisualBasic\Windows\1033 klasöründe bulunan WindowsApplication.zip dosyasından alınır.Tüm Visual Studio proje türleri için bu dosyalar <Installation Root>\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\Language klasöründe bulunabilir."VisualBasic", bu projenin Visual Basic projesi olduğunu belirtir.
    // 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 özelliğiyle elde edildikten sonra Imports kullanımını gösteren aşağıdaki kodu ekleyin.

    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 yöntemi aşağıdakileri yapmak için VSProject2 nesnesini kullanır:

    • References kullanarak System.Security.dll öğesine başvuru ekleyin.

    • Imports özelliği kullanarak Imports için bir tanıtıcı elde edin.

    Imports yöntemleri şunları yapmak için kullanılır:

    • Add kullanarak Imports koleksiyonuna System.Security ekleyin.

    • Count özelliğini kullanarak Almalar koleksiyonunda öğe sayısını görüntüleyin.

    • Item yöntemini kullanarak Imports koleksiyonunda öğelerin adlarını görüntüleyin.

    Örnek bölümünde, tüm yöntem için try-catch bloğu dahil olmak üzere tüm kod gösterilmektedir.

  10. Eklentiyi derlemek için, Derle menüsünde Çözümü Derle'yi tıklayın.

  11. Visual Studio tümleşik yazılım geliştirme ortamında (IDE) Visual Basic projesi açın.

  12. Araçlar menüsünde Eklenti Yöneticisi'ni tıklayın ve Eklenti Yöneticisi iletişim kutusundan eklentinizi seçin.Eklentinizi çalıştırmak için Tamam'ı tıklayın.

Örnek

Aşağıdaki örnek, Visual Studio otomasyonu kullanarak, Imports özelliğinin nasıl kullanılacağını gösteren temel bir Visual Studio eklentisidir.

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

Kod Derleniyor

Bu kodu derlemek için, yeni bir Visual Studio eklentisi projesi oluşturun ve OnConnection yönteminin kodunu örnekteki kodla değiştirin.Eklenti çalıştırmak hakkında daha fazla bilgi edinmek için bkz. Nasıl yapılır: Eklenti Yöneticisini Kullanarak Eklentileri Denetleme.

Ayrıca bkz.

Kavramlar

VSProject2 Nesnesine Giriş

Diğer Kaynaklar

Visual Basic ve Visual C# Projelerini Genişletme