• 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)

  • 相关阅读:
    django orm 以列表作为筛选条件进行查询
    申请Let's Encrypt通配符HTTPS证书
    redis集群部署及踩过的坑
    MySQL的索引是什么?怎么优化?
    Session管理之超时设置和强制下线
    在MySQL中使用explain查询SQL的执行计划
    基于Docker搭建MySQL主从复制
    这些年一直记不住的 Java I/O
    高并发大容量NoSQL解决方案探索
    php 如何生成静态页
  • 原文地址:https://www.cnblogs.com/bobzhangfw/p/1035688.html
Copyright © 2020-2023  润新知