Share via


VSProject2.Imports Property

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

Namespace:  VSLangProj80
Assembly:  VSLangProj80 (in VSLangProj80.dll)

Syntax

'Declaration
ReadOnly Property Imports As Imports
Imports Imports { get; }
property Imports^ Imports {
    Imports^ get ();
}
abstract Imports : Imports
function get Imports () : Imports

Property Value

Type: VSLangProj.Imports
An Imports collection, each item representing an import statement.

Implements

VSProject.Imports

Remarks

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

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."); 
    } 
}

.NET Framework Security

See Also

Reference

VSProject2 Interface

VSLangProj80 Namespace

Imports