COM MSScriptControl.ScriptControl To CodeDom

' I create a COM MSScriptControl.ScriptControl code that work ok, but it's too slow.
        'im trying to convert it to CodeDom Compiler to see if it is faster; but i dont know anything about CodeDom
        'can any one help me convert this code to CodeDom. Thanks
        Dim Code As New System.Text.StringBuilder
        Dim codeToString As String = RichTextBitch.Text
        gvSC = New MSScriptControl.ScriptControl
        With gvSC
            .Language = "VBScript"
            .SitehWnd = Me.Handle.ToInt32
            .AllowUI = True
        End With

        Code.Append(RichTextBitch.Text).AppendLine()
        Code.Append(myfunctionsWrite).AppendLine()

        gvSC.AddObject("Pimp1", class1)
        gvSC.AddObject("MySelf", class2)
        gvSC.AddObject("Pimp3", class3, True)
        gvSC.AddCode(Code.ToString)
        If gvSC.Error.Number <> 0 Then
            'MsgBox(gvSC.Error.Description, MsgBoxStyle.Information, "Processing Script")
        End If
        'My Result are in the classes
LVL 1
sayethAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Bob LearnedConnect With a Mentor Commented:
With CodeDom, you are creating text that will be compiled, so you can include text from another class into the code.

The text is collected with a StringBuilder:

     
     Dim code As New StringBuilder()
     code.Append(" Public Class EvaluatorEngine" & vbLf)

Open in new window


You could add constructs for collecting classes to include.  There are 3 collectors defined in that code for variables, references, and import statements:

  Private _VariableDeclarations As New List(Of String)()
  Private _ReferencedAssemblies As New List(Of String)()
  Private _Imports As New List(Of String)()

Open in new window

0
 
CodeCruiserCommented:
And by the way, in my knowledge, CodeDom only supports VB.NET and C# code not VBScript.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
sayethAuthor Commented:
im using VB.NET 2008.
0
 
CodeCruiserCommented:
>im using VB.NET 2008.
Is that in response to my comment http:#34905682? If so, I was referring to this

With gvSC
            .Language = "VBScript"
0
 
sayethAuthor Commented:
oh ok. is there anything i can do to make the above code go faster, for example System.Text.StringBuilder, etc.
since, that ScriptControl is the fasted approach

thanks for the help
0
 
CodeCruiserCommented:
Does the speed depend on the code? Did you try it with different code?
0
 
sayethAuthor Commented:
yes n no; It  depend on the array of information it searching through while the  Script is running.
for ex. dim infoDepth(2000) as string is fast
             dim infoDepth(3000) as string is ok
              dim infoDepth(4000) as string is slow
infoDepth is a class that i add to the Script =  gvSC.AddObject("MySelf", class2)
0
 
CodeCruiserCommented:
So it seems that the problem is with script itself. Can you show us the script?
0
 
Bob LearnedCommented:
Here is a class that I use to evaluate expressions, like the script control does.  It uses CodeDOM, and is inspired by a CodeProject article.


' Source: 
' http://www.codeproject.com/csharp/runtime_eval.asp 
' 
' VB.NET 2005 specific version converted from C# code. 

Imports Microsoft.VisualBasic
Imports System
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.Collections.Generic
Imports System.Reflection
Imports System.Text

Public Class ExpressionEvaluator

  ' Instance of the compile evaluator engine class. 
  Private _CompiledClass As Object = Nothing

  ' Variable declarations, referenced assembly, using statements, 
  ' and all evaluation expressions should be added before calling the 
  ' CompileEvaluator method. 
  Private _VariableDeclarations As New List(Of String)()
  Private _ReferencedAssemblies As New List(Of String)()
  Private _Imports As New List(Of String)()
  Private _Expressions As New Dictionary(Of String, String)()

  ' Return a list of errors. 
  Private _Errors As New List(Of String)()

  Public Sub New()
    ' Add reference to System.dll 
    _ReferencedAssemblies.Add("System.dll")

    ' Add Imports System
    _Imports.Add("System")
  End Sub

  ''' <summary> 
  ''' Use the variable declarations, referenced assemblies (i.e. System.dll, System.Xml.dll), 
  ''' using statements (i.e. using System;), and expressions to be evaluated, and compile 
  ''' them all into a single class with multiple function methods that will return an evaluation 
  ''' for each expression. 
  ''' </summary> 
  ''' <returns></returns> 
  Public Function CompileEvaluator() As Boolean
    Dim provider As CodeDomProvider = New VBCodeProvider()

    Dim parameters As New CompilerParameters()

    parameters.GenerateExecutable = False
    parameters.GenerateInMemory = True

    ' Add references system as System.Xml.dll. 
    For Each assemblyName As String In Me.ReferencedAssemblies
      parameters.ReferencedAssemblies.Add(assemblyName)
    Next

    Dim code As New StringBuilder()

    ' Add using statements to the top of the module. 
    For Each importItem As String In Me.[Imports]
      code.AppendFormat("Imports {0} " & vbLf & "", importItem.Replace("Imports", "").Trim())
    Next importItem

    ' Create the class definition. 
    code.Append(" Public Class EvaluatorEngine" & vbLf)

    ' Add private variable declarations. 
    For Each declaration As String In Me.VariableDeclarations
      code.AppendFormat(" Private {0}" & vbLf, declaration)
    Next declaration

    ' Build an evaluator method for each expression. 
    For Each key As String In Me.Expressions.Keys
      Dim expression As String = Me.Expressions(key)

      code.AppendFormat(" Public Function Evaluate{0}() As Object" & vbLf, key)
      code.AppendFormat(" Return {0}" & vbLf, expression)
      code.Append(" End Function" & vbLf)
    Next key

    code.Append(" End Class" & vbLf)

    ' Compile the evaluator engine class. 
    Dim results As CompilerResults = provider.CompileAssemblyFromSource(parameters, code.ToString())

    If results.Errors.HasErrors Then
      For Each err As CompilerError In results.Errors
        _Errors.Add(err.ErrorText)
        ' Add each compiler error to a list. 
      Next
    Else
      ' Create an instance of the evaluator engine class. 
      Dim assy As Assembly = results.CompiledAssembly
      _CompiledClass = assy.CreateInstance("EvaluatorEngine")
    End If

    ' Return the compiler success status. 
    Return Not results.Errors.HasErrors
  End Function

  Public ReadOnly Property VariableDeclarations() As List(Of String)
    Get
      Return _VariableDeclarations
    End Get
  End Property

  Public ReadOnly Property ReferencedAssemblies() As List(Of String)
    Get
      Return _ReferencedAssemblies
    End Get
  End Property

  Public ReadOnly Property [Imports]() As List(Of String)
    Get
      Return _Imports
    End Get
  End Property

  Public ReadOnly Property Errors() As List(Of String)
    Get
      Return _Errors
    End Get
  End Property

  Public ReadOnly Property Expressions() As Dictionary(Of String, String)
    Get
      Return _Expressions
    End Get
  End Property

  ''' <summary> 
  ''' Use a key to find an evaluation expression method in the evaluator engine class, 
  ''' and invoke the method to return a result. 
  ''' </summary> 
  ''' <example> 
  ''' Dim eval As New ExpressionEvaluator()
  ''' 
  ''' eval.VariableDeclarations.Add("a As Integer = 5") 
  ''' eval.VariableDeclarations.Add("b As Integer = 10")
  ''' 
  ''' eval.Expressions.Add("Sum", "a + b")
  ''' 
  ''' If eval.CompileEvaluator() Then
  '''   Dim result As Integer = Val(eval.Evaluate("Sum"))
  ''' End If
  ''' </example> 
  ''' <param name="expressionKey">The key to the expression to be evaluated</param> 
  ''' <returns>The evaluation result</returns> 
  Public Function Evaluate(ByVal expressionKey As String) As Object
    If _CompiledClass Is Nothing Then
      Throw New ApplicationException("Evaluator not compiled.  Please use CompileEvaluator first.")
    End If
    If Me.Expressions.ContainsKey(expressionKey) Then
      Dim method As MethodInfo = _CompiledClass.GetType().GetMethod("Evaluate" + expressionKey)
      Return method.Invoke(_CompiledClass, Nothing)
    End If
    Return Nothing
  End Function

End Class

Open in new window

0
 
sayethAuthor Commented:
Thanks again guys.

To CodeCruiser: the script depend on the custom. but i have lots of pre build functions the custom might use. for example.

A 'variable
B 'variable
Do While GetData    'getdata is a prebuild function that get each data in  infoDepth(2000)  

  A = Null
  B = Null
  Do
    If G = Null Then Exit Do 'G is a preBuild variable
    V= Result(A,B,G)  ' V is a preBuild variable n Result is a prebuild function
    Exit Do
  Loop
Loop


To TheLearnedOne: Nice;
Is it possible to add object in CodeDom like in COM MSScriptControl.ScriptControl
for Ex.

dim class1 as new classSumThing
gvSC = New MSScriptControl.ScriptControl
gvSC.AddObject("MySelf", class1)
'gvSC.AddObject("Pimp3", class3, True)


Loop
0
 
sayethAuthor Commented:

Thanks a lot guys.
0
 
CodeCruiserCommented:
I thought you were working with VBScript!
0
All Courses

From novice to tech pro — start learning today.