Visual Studio for Application insider 3
HostObject is an important function of vsa, which is also its previous vba sdk, including Microsoft Script Control or other third-party vb language implementation (winwrap basic, basicscript, sbl and other important functions)
HostObject is such an object that can be used without declaration in the script. For example, you can use application in excel and Server and Session in asp, you can use objects such as Document, Site, and Category in ActiveContent templates.
To facilitate your understanding, we create a Document class
Public Class Document
Private _ title As String
Private _ submitDate As Date
Private _ url As String
Private _ content As String
Public Sub New ()
End Sub
Public Sub New (ByVal title As String, ByVal content As String, ByVal url As String, ByVal submitDate As Date)
Me. Title = title
Me. content = content
Me. url = URL
Me. submitdate = submitdate
End sub
Public property content () as string
Get
Return _ content
End get
Set (byval value as string)
_ Content = Value
End set
End Property
Public property title () as string
Get
Return _ title
End Get
Set (ByVal Value As String)
_ Title = Value
End Set
End Property
Public Property SubmitDate () As Date
Get
Return _ submitDate
End Get
Set (ByVal Value As Date)
_ SubmitDate = Value
End Set
End Property
Public Property Url () As String
Get
Return _ url
End Get
Set (ByVal Value As String)
_ Url = Value
End Set
End Property
End Class
Compile this class
Vbc/out: bin \ document. dll/t: library
Reference this document. dll in the project
We still need to use the MyVsaSite class
We declare a class-level variable in it.
Private doc as Document
And initialize it in the new process.
Public Sub New ()
Doc = New Document ("vsa insider 3", "vsa insider 3", "http://www.soho-works.net/blog/275.asp", Now)
End Sub
Let's modify the Script to use this Document object.
Class TestClass
Public shared sub Hello (byval name as string)
System. Windows. Forms. MessageBox. Show (ThisDocument. Title)
End sub
End Class
How to call it?
1. Create an IVsaReferenceItem instance
Dim ReferenceItem As Microsoft. Vsa. IVsaReferenceItem
ReferenceItem = m_VsaEngine.Items.CreateItem ("Document", Microsoft. Vsa. VsaItemType. Reference, Microsoft. Vsa. VsaItemFlag. None)
ReferenceItem. AssemblyName = "C: \ Documents ents and Settings \ Administrator \ My Documents ents \ Visual Studio Projects \ WindowsApplication2 \ WindowsApplication2 \ bin \ Document. Dll"
2. Create an IVsaGlobalItem instance
Dim GlobalItem As Microsoft. Vsa. IVsaGlobalItem
GlobalItem = m_VsaEngine.Items.CreateItem ("ThisDocument", Microsoft. Vsa. VsaItemType. AppGlobal, Microsoft. Vsa. VsaItemFlag. None)
GlobalItem. TypeString = "Document"
Note: TypeString is Document
2. Implement GetGlobalInstance of MyVsaSite
Public Function GetGlobalInstance (ByVal name As String) As Object Implements Microsoft. Vsa. IVsaSite. GetGlobalInstance
Select Case name. ToLower
Case "thisdocument"
Return doc
End Select
End Function
3. The call method has not changed.
Dim args () As Object = New Object () {"jjx "}
Invoke ("TestClass. Hello", args)