Reference 接口
更新:2007 年 11 月
表示项目中的一个引用。在项目中包含引用,使您可以使用该引用中包含的任意公共成员。项目可以包含对其他 .NET 项目、.NET 程序集和 COM 对象的引用。
命名空间: VSLangProj
程序集: VSLangProj(在 VSLangProj.dll 中)
语法
声明
<GuidAttribute("35D6FB50-35B6-4C81-B91C-3930B0D95386")> _
Public Interface Reference
用法
Dim instance As Reference
[GuidAttribute("35D6FB50-35B6-4C81-B91C-3930B0D95386")]
public interface Reference
[GuidAttribute(L"35D6FB50-35B6-4C81-B91C-3930B0D95386")]
public interface class Reference
public interface Reference
备注
Reference 对象包含在 VSProject 对象的 References 集合中。Reference 对象有两种类型:程序集(包括 Visual Studio 项目)和 COM 对象。当引用是另一个项目时,称为项目到项目的引用,但仍视为程序集引用。
示例
下面的示例从模板创建一个新项目,然后添加两个引用,并显示引用的类型。
'Macro Editor
Imports VSLangProj
Sub NewProject()
Dim newName As String = InputBox("New project name:")
' Create a new project in the solution based on an existing
' project.
Dim newProject As Project = DTE.Solution.AddFromTemplate( _
"C:\TemplatePath\Template.vbproj", _
"C:\ProjectPath\" & newName, newName)
' Add a COM reference and display its type.
Dim vsProject As VSProject = CType(newProject.Object, VSProject)
Dim newRef As Reference
newRef = vsProject.References.Add("C:\WINNT\System32\msmask32.ocx")
MsgBox(GetRefTypeName(newRef))
' Add an Assembly reference and display its type, "Assembly".
newRef = vsProject.References.Add("C:\SomeProject\bin\SomeProject.dll")
MsgBox(GetRefTypeName(newRef))
End Sub
Private Function GetRefTypeName(ByVal ref As Reference) _
As String
Dim type As String
Select Case ref.Type
Case prjReferenceType.prjReferenceTypeActiveX
type = "COM"
Case prjReferenceType.prjReferenceTypeAssembly
type = "Assembly"
End Select
Return type
End Function
下面的示例创建某个引用属性的简短报告。
' Macro Editor
' Create a small report about a reference.
Imports VSLangProj
Function ReportReferences(ByVal aRef As Reference) As String
Dim report As String = ""
Dim type As String
' Each entry in the ArrayList will contain a label and a value.
Dim ht As System.Collections.ArrayList = _
New System.Collections.ArrayList()
With aRef
ht.Add(New String() {"Name", .Name})
ht.Add(New String() {"Description", .Description})
ht.Add(New String() {"Version", String.Format("{0}.{1}.{2}.{3}", _
.MajorVersion, .MinorVersion, .BuildNumber, .RevisionNumber)})
ht.Add(New String() {"Location", .ContainingProject.FullName})
Select Case .Type
Case prjReferenceType.prjReferenceTypeActiveX
type = "COM"
Case prjReferenceType.prjReferenceTypeAssembly
type = "Assembly"
End Select
ht.Add(New String() {"Type", type})
ht.Add(New String() {"Culture", .Culture})
End With
Dim datas() As String
For Each datas In ht
report &= datas(0) & ControlChars.Tab & datas(1) & ControlChars.CrLf
Next
Return report
End Function