Visual Studio for Application

Source: Internet
Author: User
Visual Studio for Application

There are very few documents about vsa, Which is why I decided to write some introduction. Of course, this is just my understanding of vsa. It may be wrong, and it may not be suitable for beginners.

Due to the large amount of content, we plan to introduce it multiple times.

  • Overview
  • Load Pre-compiled code
  • Hostobject
  • VSA ide
  • Vsa sdk (multiple times possible)

In the previous section, we do not use the vsa sdk. In fact, all functions can be implemented without the vsa sdk. In the following section, we will detail the Vsa sdk (mainly taking beta 2 as an example, of course, I also want to introduce vsa sdk versions of Vs.net 2003 that have been accidentally discovered)

Related namespaces

First, let's check the namespace about vsa and find that
Microsoft. Vsa (Microsoft. Vsa. Dll)
Microsoft. VisualBasic. Vsa (Microsoft. VisualBasic. Vsa. Dll)
Microsoft. JScript. Vsa (Microsoft. JScript. Vsa. Dll)
Microsoft_VsaVb (Microsoft_VsaVb.dll)
And so on
It is difficult for vsa developers to understand their internal relationships. In my understanding
The vsa function is actually implemented based on com. vsavbdt. dll is designed for vsa, while vsavb7rt. dll is run for vsa.

Microsoft. Vsa defines basic interfaces, enumeration, and exception
What is important here is the VsaLoader class
VsaLoader is mainly used to load and execute pre-compiled vsa code. It only implements some IVsaEngine interfaces and runs very fast. In the second part, we will introduce how to use VsaLoader to load pre-compiled Vsa code.

Microsoft_VsaVb is implemented by com. net packaging, view the Microsoft_VsaVB code, you will find that it mainly uses ComImportAttribute, Microsoft_VsaVB defines VsaDTEngine interface (implement IVsaDTEngine Interface), VsaDTEngineClass class (implement VsaDTEngine Interface ), vsaEngine interface (implementing the IVsaEngine Interface) and VsaEngineClass (implementing the VsaEngine Interface)

Visual Basic. Vsa and Microsoft. JScript. Vsa are the specific implementations of vb.net and JScript respectively.

Understanding the concept of Site

Site is equivalent to a place. VsaEgnine has the IVsaSite interface attribute Site. It means that we need to create a class that implements the IVsaSite interface and accept the class that is triggered from VsaEngine, such as policy, OnCompilerError, provide values to the objects that implement the IVsaEngine interface, such as GetCompiledState and GetGlobalInstance.

IVsaSite is very important. We will talk about it later.
For example:
Read pre-compiled code
Hostobject

Create a class to implement ivsasite

Imports Microsoft. vsa
Class MyVsaSite
Implements IVsaSite
.....

End class

Task scenario: use Microsoft. VisualBasic. VSA. vsaengine to dynamically load a VB.net class and run

This is the most common task.

In this example, Textbox1 has a class Code. The Code is as follows:

Class TestClass
Public shared sub Hello (byval name as string)
System. Windows. Forms. MessageBox. Show (name)
End sub
End Class

1. Reference Microsoft. Vsa. dll, Microsoft. VisualBasic. vsa. dll
2,
Imports Microsoft. Vsa
Imports Microsoft. VisualBasic. Vsa
3. Define IVsaEngine variables at the module level
Dim m_VsaEngine as IVsaEngine

4. Create an instance of Microsoft. VisualBasic. Vsa. VsaEngine
M_VsaEngine = new Microsoft. VisualBasic. Vsa. VsaEngine ()
5. Set RootNameSpace and RootMoniker
M_VsaEngine.RootNameSpace = "Test"
M_VsaEngine.RootMoniker = "myapp: // Project1"

6. Set Site attributes (note: the site must be set after RootMoniker)

M_VsaEngine.Site = new MyVsaSite

7. Create an IVsaCodeItem instance
Dim codeItem As VsaCodeItem
CodeItem = m_VsaEngine.Items.CreateItem ("test", Microsoft. Vsa. VsaItemType. Code, Microsoft. Vsa. VsaItemFlag. Class)
CodeItem. AppendSourceText (TextBox1.Text)
8. Compile and run
If m_VsaEngine.Compile () Then
M_VsaEnigne.Run ()
End If
9. The call is implemented through Reflection. to simplify the call process, I used vb.net to simulate the Runtime class Invoke process in the vsa sdk.

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

Invoke ("TestClass. Hello", args)

Vb.net Implementation of Invoke
 

Public Overloads Function Invoke (ByVal methodName As String, ByVal arguments () As Object
If methodName Is Nothing OrElse String. Compare (methodName, "") = 0 Then
Throw New ArgumentNullException ("methodName ")

End If
If v Is Nothing Then

End If
If Not m_VsaEngine.IsRunning Then
M_VsaEngine.Run ()

End If
Dim chs2 () As Char = New Char (){"."}
If methodName. Split (chs2). Length <2 Then
Throw New Exception ("")

End If
Dim I As Integer = methodName. LastIndexOf (".")
Dim str1 As String = methodName. Substring (0, I)
Dim str2 As String = methodName. Substring (I + 1)
Dim str4 = String. Concat (ScriptEngine. RootNamespace, ".", str1)
Dim methodInfo As MethodInfo = ScriptEngine. Assembly. GetType (str4, True, True). GetMethod (str2)
If Not methodInfo Is Nothing Then
Return methodInfo. Invoke (Nothing, arguments)

End If

End Function

 

Compilation Error
Oncompilererror that implements the ivsasite interface can be used to understand the compilation error information.

Public Function oncompilererror (byval [Error] as Microsoft. VSA. ivsaerror) as Boolean implements Microsoft. VSA. ivsasite. oncompilererror
Debug. writeline ([Error]. description)

End Function

Cause: http://www.soho-works.net/blog/post/464.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.