Visual Studio for Application 内幕之三

Visual Studio for Application 内幕之三

vsa的一个重要功能就是HostObject,这也是其上代vba sdk,包括Microsoft Script Control或是其他第三方vb语言实现(winwrap basic,basicscript,sbl等的重要功能)

hostObject是这样的一个对象,在script中无需声明就可以使用,比方说在excel中你可以使用application,在asp中你可以使用Server,Session等,包括在ActiveContent的模板中你可以使用Document,Site,Category等对象 :),都是类似的应用。

为了便于大家理解,我们创建一个Document类

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


编译该类
vbc /out:bin\document.dll /t:library
在项目中引用该document.dll

我们依旧要借助MyVsaSite 类
我们在其中声明一个类级变量
private doc as Document

并在new 过程中对其进行初始化
    Public Sub New()
        doc = New Document("vsa 内幕三", "vsa 内幕三", "http://www.soho-works.net/blog/275.asp", Now)
    End Sub

我们修改一下Script ,使其使用这个Document对象
class TestClass
 public shared sub Hello(byval name as string)
  
  System.Windows.Forms.MessageBox.Show(ThisDocument.Title)
 end sub
End Class

如何调用?
1、创建一个IVsaReferenceItem实例

Dim ReferenceItem As Microsoft.Vsa.IVsaReferenceItem
        ReferenceItem = m_VsaEngine.Items.CreateItem("Document", Microsoft.Vsa.VsaItemType.Reference, Microsoft.Vsa.VsaItemFlag.None)

        ReferenceItem.AssemblyName = "C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\WindowsApplication2\WindowsApplication2\bin\Document.Dll"


 

2、创建一个IVsaGlobalItem实例
   Dim GlobalItem As Microsoft.Vsa.IVsaGlobalItem
        GlobalItem = m_VsaEngine.Items.CreateItem("ThisDocument", Microsoft.Vsa.VsaItemType.AppGlobal, Microsoft.Vsa.VsaItemFlag.None)
        GlobalItem.TypeString = "Document"

注意:TypeString 为Document
2、实现MyVsaSite的GetGlobalInstance
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、调用方法没有改变

  Dim args() As Object = New Object() {"jjx"}

        Invoke("TestClass.Hello", args)

你可能感兴趣的:(application)