Partilhar via


VSProject2.Imports Property

Definition

Gets the Imports object associated with the project. For C# projects, the Imports property is set to Nothing (a null reference).

public:
 property VSLangProj::Imports ^ Imports { VSLangProj::Imports ^ get(); };
[System.Runtime.InteropServices.DispId(11)]
public VSLangProj.Imports Imports { [System.Runtime.InteropServices.DispId(11)] get; }
[<System.Runtime.InteropServices.DispId(11)>]
[<get: System.Runtime.InteropServices.DispId(11)>]
member this.Imports : VSLangProj.Imports
Public ReadOnly Property Imports As Imports

Property Value

An Imports collection, each item representing an import statement.

Implements

Attributes

Examples

To run this example as an add-in, see How to: Compile and Run the Automation Object Model Code Examples. Open a Visual Basic project before running this example.

[Visual Basic]

' Add-in  
Imports VSLangProj  
Imports VSLangProj80  

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)  
    ListImports(applicationObject)  
End Sub  

Sub ListImports(ByVal dte As DTE2)  
    ' The first project is a Visual Basic or C# project.  
    Dim vsproject As VSProject2 = _  
    CType(applicationObject.Solution.Projects.Item(1).Object,_  
    VSProject2)  

    Dim projImports As VSLangProj.Imports = vsproject.Imports  
        ' For C# projects, projImports will be Nothing.  
    If Not (projImports Is Nothing) Then  
        Dim i As Integer  
        Dim str As String  
        str = ""  
        For i = 1 To projImports.Count  
            str = (str & str.Concat(projImports.Item(i)) & vbCrLf)  
         Next  
        MsgBox(str)  
    Else  
        MsgBox("This project has no imports.")  
    End If  
End Sub  

[C#]

using System.Windows.Forms;  
using VSLangProj;  
using VSLangProj2;  
using VSLangProj80;  
public void OnConnection(object application, ext_ConnectMode  
 connectMode, object addInInst, ref Array custom)  
{  
    applicationObject = (DTE2)application;  
    addInInstance = (AddIn)addInInst;  
ListImports((DTE2)applicationObject);  
}  
public void ListImports( DTE2 dte )   
{   
// The first project is a Visual Basic or C# project.  
    VSProject2 vsproject = ( ( VSProject2 )  
(applicationObject.Solution.Projects.Item( 1 ).Object ) );   
    VSLangProj.Imports projImports = vsproject.Imports;   
    // For C# projects, projImports will be Nothing.  
    if ( !(  projImports == null ))   
    {   
        int i = 0;   
        string str = null;   
        string str1 = projImports.Count.ToString();  
        MessageBox.Show("The number of Imports is: " + str1);   
        MessageBox.Show( projImports.ContainingProject.Name );   
        str = "";   
        for ( i=1; i<=projImports.Count; i++ )   
        {   
            str = ( str + String.Concat( projImports.Item( i ) )  
 + "\n" );   
        }   
        MessageBox.Show( str);   
    }   
    else   
    {   
        MessageBox.Show( "This project has no imports.");   
    }   
}  

Remarks

C# projects do not have project imports. For C# projects, the value of Imports is Nothing (a null reference).

Applies to