VSProject2.Imports 属性
获取与项目关联的 Imports 对象。 对于 C# 项目,将 Imports 属性设置为 Nothing(nullnull 引用(在 Visual Basic 中为 Nothing) 引用)。
命名空间: VSLangProj80
程序集: VSLangProj80(在 VSLangProj80.dll 中)
语法
声明
ReadOnly Property Imports As Imports
Imports Imports { get; }
property Imports^ Imports {
Imports^ get ();
}
abstract Imports : Imports
function get Imports () : Imports
属性值
类型:VSLangProj.Imports
一个 Imports 集合,其中每项表示一个导入语句。
备注
C# 项目没有项目导入。 对于 C# 项目,Imports 的值为 Nothing(nullnull 引用(在 Visual Basic 中为 Nothing) 引用)。
示例
若要将此示例作为外接程序运行,请参见如何:编译和运行自动化对象模型代码示例。 在运行此示例之前,请打开一个 Visual Basic 项目。
[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 安全性
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。