Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Scripting .NET ready, except...
Message
 
To
All
General information
Forum:
ASP.NET
Category:
Common Language Runtime
Title:
Scripting .NET ready, except...
Miscellaneous
Thread ID:
00697293
Message ID:
00697293
Views:
60
Hi, with the help of MSDN Help and the guys here (thanks) i a build a class for doing things like:

MyValue=oEvaluator.Evaluate(" ""The date is ""+DateTime.Now().ToString()")

This work well, except becuase when this evaluation script generate a error (for example for a syntax error) i don't get it, or yes. This use the class from Visual Studio For Aplications and when a error is executed, is notified the site class (the site class is the object where the script is controled, or somenthing like). The OnError handler exceute, but the error is not propagated to the caller (the asp.net page). The design is based in the MSDN paper "Script happens .NET" and use the same things, this is code (enjoy if you want for you:)

For test, add the assemblies marked with .VSA, put the code in a file called VBScript.vb, and write this:

Dim oEval As New TVBScript()
Dim oValor as Object

oValor=oEval("DateTime.Now()")

*--- CODE:

Imports Microsoft.VisualBasic.Vsa
Imports System.Reflection

Public Class TVBScript
Private FEngine As Microsoft.VisualBasic.Vsa.VsaEngine
Private FItems As Microsoft.Vsa.IVsaItems
' Code Item that stores the script
Private FScript As Microsoft.Vsa.IVsaCodeItem
' temporary Src string
Private FTempSrc As String
' VSA reference Item for holding the system.dll reference
Private FSystemRef As Microsoft.Vsa.IVsaReferenceItem
' temporary reference used in the AddReference method
Private FTempRef As Microsoft.Vsa.IVsaReferenceItem
' String that contains any imports statements needed for the addobject
Private FImportsScript As String
Private FImports As New Collections.Specialized.NameValueCollection()

Public lError As Boolean = False

Public Function AgregarReferencia(ByVal Nombre As String, ByVal NombreAssembly As String) As Boolean
FImports.Add(Nombre, NombreAssembly)
FTempRef = FItems.CreateItem(Nombre, Microsoft.Vsa.VsaItemType.Reference, Microsoft.Vsa.VsaItemFlag.None)
' Set the assemblyname
FTempRef.AssemblyName = NombreAssembly

End Function

Public Function Evaluar(ByVal CodigoEvaluar As String) As Object
' Generar la evaluacion...
Dim i As Integer
Dim ModType As Type
Dim method As MethodInfo
Dim Argumentos() As Object
Dim oResultado As Object

'1: Agregar los assemblys
For i = 0 To FImports.Count - 1
FTempSrc = FTempSrc + "Imports " + FImports.Keys.Item(i) + vbCrLf
Next
' Una linea en blanco
FTempSrc = FTempSrc + vbCrLf + "Module scriptcode" + vbCrLf
' Y crear un metodo
FTempSrc = FTempSrc + " Public Function EvaluarFuncion() As Object" + vbCrLf + " Return " + CodigoEvaluar + vbCrLf + " End Function"
FTempSrc = FTempSrc + vbCrLf + "End Module"

' Compilar el codigo
' Check to see if the engine is running
If FEngine.IsRunning Then
' If it is reset the engine
FEngine.Reset()
End If
' Asignar el codigo

FScript.SourceText = FTempSrc

lError = True
'Try
If FEngine.Compile() Then
FEngine.Run()

' Get the type from the assembly
ModType = FEngine.Assembly.GetType(FEngine.RootNamespace + ".scriptcode", True, True)
' Obtener el metodo
method = ModType.GetMethod("EvaluarFuncion")
oResultado = method.Invoke(Nothing, Argumentos)
lError = False

Return oResultado
End If
'Catch e As Exception
' Throw New Exception(e.Message)
'End Try

Return Nothing
End Function

Public Sub New()
MyBase.New()

' Los siguientes metodos deben hacerce en estricto orden...
FEngine = New Microsoft.VisualBasic.Vsa.VsaEngine()
FEngine.RootMoniker = "com.gyg://gygsoftware.com"
FEngine.Site = New VsaSiteScript()
FEngine.RootNamespace = "EvaluadorVBScript"
'FEngine.LoadSourceState(FEngine.Site)
' Inicializar la engine de script
FEngine.InitNew()
FItems = FEngine.Items
' Generar el modulo que contiene el script...
FScript = FItems.CreateItem("scriptcode", Microsoft.Vsa.VsaItemType.Code, Microsoft.Vsa.VsaItemFlag.Module)
' Por defecto, agregar el assembly system
AgregarReferencia("System", "System.dll")
End Sub
End Class

' Simple implementation of a VSA Site for use by the VSAScriptHost Class
Public Class VsaSiteScript
Implements Microsoft.Vsa.IVsaSite
' Hashtable to store the instances of the objects being added to the engine
Private eventInstances As New Hashtable()
Public Function AddEvent(ByVal Name As String, ByVal type As String, ByRef Instance As Object, ByVal Events As Boolean)
'Try
eventInstances.Add(Name, Instance)
'Catch e As Exception
' MsgBox("Couldn't add event instance to the hashtable")
'End Try
End Function

Public Sub GetCompiledState(ByRef pe() As Byte, ByRef debugInfo() As Byte) Implements Microsoft.Vsa.IVsaSite.GetCompiledState

End Sub

Public Function GetEventSourceInstance(ByVal itemName As String, ByVal eventSourceName As String) As Object Implements Microsoft.Vsa.IVsaSite.GetEventSourceInstance
'Try
Return eventInstances(eventSourceName)
'Catch e As Exception
' Throw New Exception("couldn't find the instance")
' End Try
End Function

Public Function GetGlobalInstance(ByVal [name] As String) As Object Implements Microsoft.Vsa.IVsaSite.GetGlobalInstance

End Function

Public Sub Notify(ByVal notify As String, ByVal info As Object) Implements Microsoft.Vsa.IVsaSite.Notify

End Sub

Public Function OnCompilerError(ByVal [error] As Microsoft.Vsa.IVsaError) As Boolean Implements Microsoft.Vsa.IVsaSite.OnCompilerError
' Provide a simple dialog with the compile error message
Throw New Exception([error].Description & "on line " & [error].Line + vbCrLf + " Linea:" + [error].LineText)
'[error].Description & "on line " & [error].Line + vbCrLf + " Linea:" + [error].LineText
End Function
End Class
The Life is Beautiful!

Programmer in
Delphi, VS.NET
MCP
Reply
Map
View

Click here to load this message in the networking platform