Author Topic: VB.NET Evaluation of a VB.net Expression  (Read 23512 times)

0 Members and 1 Guest are viewing this topic.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: VB.NET Evaluation of a VB.net Expression
« Reply #30 on: June 01, 2010, 01:37:53 PM »
I suppose its a good thing that you don't make all the rules ... or that any one person doesn't .. it would be a pretty dull place to live

Sometimes I really wonder about the posts I read here...  Some of them get pretty far out there.

Well, wonder no more ... would you like to be appointed the one and only rule maker? ;-)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Peter Jamtgaard

  • Guest
Re: VB.NET Evaluation of a VB.net Expression
« Reply #31 on: June 01, 2010, 05:20:11 PM »
Group,

I sure enjoy the dialog when it comes to LISP, there are a lot of strong emotions running.

LISP is cool, so are the other languages...IMHO, its all good.



My lecture proposal is still pending.

It is a LOT of work to write it up and present it.

For the small stipend...

What is important is that I learn how to do it and then share what I learn with everyone...somehow.

Peter




Peter Jamtgaard

  • Guest
Re: VB.NET Evaluation of a VB.net Expression
« Reply #32 on: June 18, 2010, 08:08:31 AM »
Here's an article I found with a quick Google search Pete.

http://www.eggheadcafe.com/articles/20030908.asp

I 'think' I know where you're headed with this.  Let us know how it turns out.

I got this one to work

(EvaluateVB "MsgBox (\"Hello World\")")

It does create a unknown assembly so will eat up memory.

But it works for a compile at runtime function. You can also import a file.vb file too if you play with a couple methods.

A certain expert I corresponded with regarding this function said.

"Compiler as a Service is going to be a key feature in .NET 5"

so something like this will be available in a couple years without the baggage.

I thought that was good news.

Peter

It is a modified form of the original code on eggheadcafe that was recommended by Bobby C. Jones See link above.

Code: [Select]
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.GraphicsInterface
Imports Autodesk.AutoCAD.Runtime
Imports Microsoft.VisualBasic
Imports System
Imports System.Text
Imports System.CodeDom.Compiler
Imports System.Reflection
Imports System.IO



Public Class EvaluatorVBClass

' Syntax (EvaluateVB "MsgBox (\"Hello World\")")

    <LispFunction("EvaluateVB")> _
    Public Function EvaluateVB(ByVal rbfLISPArguments As ResultBuffer) As TypedValue
        Dim arrLISPArguments As TypedValue() = rbfLISPArguments.AsArray
        Dim objReturn As Object = Evaluate(arrLISPArguments(0).Value.ToString)
        If Not objReturn = Nothing Then
            Select Case (objReturn.GetType.ToString)
                Case "System.Int16"
                    Return New TypedValue(5003, objReturn)
                Case "System.Int32"
                    Return New TypedValue(5010, objReturn)
                Case "System.String"
                    Return New TypedValue(5005, objReturn)
                Case "System.Double"
                    Return New TypedValue(5001, objReturn)
            End Select
        End If

        Return New TypedValue(5019, -1)

    End Function
    Public Function Evaluate(ByVal vbCode As String) As Object
        'Dim objVBCodeProvider As VBCodeProvider = New VBCodeProvider


        Dim objCodeDomProvider As CodeDomProvider = CodeDomProvider.CreateProvider("VisualBasic")
        Dim objCompilerParameters As CompilerParameters = New CompilerParameters()

        objCompilerParameters.GenerateInMemory = True
        ' objCompilerParameters.OutputAssembly = "C:\Documents and Settings\peter.CORDECKSALES\My Documents\Visual Studio 2008\Projects\Evaluation\EvaluateVB\EvaluateVB\bin\Debug\testeval.dll"

        objCompilerParameters.ReferencedAssemblies.Add("system.dll")
        'objCompilerParameters.ReferencedAssemblies.Add("c:\yourProjectDir\bin\YourBaseClass.dll")

        objCompilerParameters.CompilerOptions = "/t:library"

        Dim objStringBuilder As StringBuilder = New StringBuilder("")

        objStringBuilder.Append("Imports System" & vbCrLf)
        objStringBuilder.Append("Imports Microsoft.VisualBasic" & vbCrLf)

        objStringBuilder.Append("Namespace Code  " & vbCrLf)
        objStringBuilder.Append("Class Library " & vbCrLf)
        objStringBuilder.Append("public function  EvalCode() as Object " & vbCrLf)
        'objStringBuilder.Append("Dim objResult as Object " & vbCrLf)
        'sb.Append("YourNamespace.YourBaseClass thisObject = New YourNamespace.YourBaseClass()")
        objStringBuilder.Append("Dim objResult as Object = " & vbCode & vbCrLf)

        objStringBuilder.Append("Return objResult" & vbCrLf)
        objStringBuilder.Append("End Function " & vbCrLf)
        objStringBuilder.Append("End Class " & vbCrLf)
        objStringBuilder.Append("End Namespace" & vbCrLf)

        Dim objCompilerResults As CompilerResults
        '
        objCompilerResults = _
            objCodeDomProvider.CompileAssemblyFromSource(objCompilerParameters, objStringBuilder.ToString())


        If objCompilerResults.Errors.Count > 0 Then
            ' Display compilation errors.
            Dim objCompilerError As CompilerError
            For Each objCompilerError In objCompilerResults.Errors
                MsgBox(objCompilerError.ToString())
            Next objCompilerError
        Else
            Dim objAssembly As System.Reflection.Assembly = objCompilerResults.CompiledAssembly
            Dim objInstance As Object = objAssembly.CreateInstance("Code.Library")
            Dim objType As Type = objInstance.GetType()
            Dim objMethodInfo As MethodInfo = objType.GetMethod("EvalCode")
            Return objMethodInfo.Invoke(objInstance, Nothing)
        End If
        Return Nothing
    End Function

End Class__________________