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

0 Members and 1 Guest are viewing this topic.

Peter Jamtgaard

  • Guest
VB.NET Evaluation of a VB.net Expression
« on: May 26, 2010, 12:10:46 PM »
Hi Group,

I want to pass a string to a vb.net sub and have it convert the string to a vb.net expression and then evaluate it

Like the vbastmt in AutoCAD

Like this LISP expression: (eval (read "(setq x 1)"))

It reads (converts) the string to an expression and then evaluates it.

Thanks

Ken Alexander

  • Newt
  • Posts: 61
Re: VB.NET Evaluation of a VB.net Expression
« Reply #1 on: May 26, 2010, 01:36:06 PM »
There is no equivalent in .NET.  In fact, if you really think about it, that concept doesn’t relate to .NET and OOP. 

You have properties and methods that you can ‘get to’ with strings using Reflection.  Other than that, your question does not give enough information on what you are trying to accomplish to provide any other help.
Ken Alexander

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8696
  • AKA Daniel
Re: VB.NET Evaluation of a VB.net Expression
« Reply #2 on: May 26, 2010, 01:59:50 PM »

Peter Jamtgaard

  • Guest
Re: VB.NET Evaluation of a VB.net Expression
« Reply #3 on: May 26, 2010, 02:31:57 PM »
Hey Ken,

Other than that, your question does not give enough information on what you are trying to accomplish to provide any other help.

Let  me try to elaborate.

Say if I were to run the vbastmt command in AutoCAD (when vba is available) and I were to pass the function this string:

msgbox(thisdrawing.name)

A message box would appear and say "Drawing1.dwg" for example...

The statement is a string.

The string is converted to a vba expression and then evaluated (run)

I want to know if there is a way to do the same thing with vb.net.

I pass a vb.net expression as a string argument to some (unknown vb.net expression), and it converts the string to an vb.net expression and evaluates it.

More sense?

Peter


Bobby C. Jones

  • Swamp Rat
  • Posts: 516
  • Cry havoc and let loose the dogs of war.
Re: VB.NET Evaluation of a VB.net Expression
« Reply #4 on: May 26, 2010, 03:08:48 PM »
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.
Bobby C. Jones

Peter Jamtgaard

  • Guest
Re: VB.NET Evaluation of a VB.net Expression
« Reply #5 on: May 26, 2010, 03:56:19 PM »
Very interesting...
 :-o
Peter

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: VB.NET Evaluation of a VB.net Expression
« Reply #6 on: May 26, 2010, 04:12:11 PM »
OOOH .. I can see lots of neat thingies for that little gem ...

can we say code validation without compiling the entire thing .... select a piece of complex code, run it through the evaluator and see if the returned object is what is expected ... on the fly ...

yep ... I gotta keep this puppy
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

sinc

  • Guest
Re: VB.NET Evaluation of a VB.net Expression
« Reply #7 on: May 26, 2010, 05:22:44 PM »
I'm not so sure about that...  It looks like it basically compiles and netloads the assembly that is created "on the fly", which indicates we'd have all the usual problems with the inability to unload an assembly once it is loaded...  It also seems very "heavy handed"...  I'm not sure what sort of impact this approach might have, once the application has been running for a while.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: VB.NET Evaluation of a VB.net Expression
« Reply #8 on: May 26, 2010, 05:49:43 PM »
why couldn't you netload the assembly, the assembly would then netload the code in a wrapper (we should be able to capture the wrapper and use it to unload it) then simpley use is as expected .. certainly you wouldn't use it while actually using AutoCAD .. it would be a testing environment at best ..

besides, lately I have been doing a heap of stuff that isn't cad related .. data crunching, database programming, GIS calculations and statistical analysis software ... I can see this being useful right in the IDE if developed properly ...
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 #9 on: May 26, 2010, 06:05:13 PM »
This is a really interesting discussion.

(I would be interested in the disposable wrapper for the loading of a net application you mentioned to unload the app.)

I had hoped that it would be fairly straight forward to evaluate a vb.net statement inside .net.

Another question, I want to demand load vb.net dll's and avoid the netload command pipe.

Would that example provide a way to do that or do one of you have an example of a routine that does that?

Peter

Peter Jamtgaard

  • Guest
Re: VB.NET Evaluation of a VB.net Expression
« Reply #10 on: May 27, 2010, 09:07:49 AM »
Hey Group,

In ther example above I noticed the CodeDOM include.

I did a web search on it and found this example over on the vbforums.

http://www.vbforums.com/showthread.php?t=397265

By Conipto.

It works pretty good and evaluates VB statements.

I am not sure if it eveluates VB.net statements.

Any thoughts would be greatly appreciated?

Peter


Code: [Select]
Imports System.CodeDom.Compiler
Imports Microsoft.VisualBasic 'More than just VB6 Runtime functions in here!
Imports System.Reflection
And here is the function, converted to VB.NET

VB Code:
Code: [Select]
''' <summary>
    ''' A simple function using CodeDom to process an expression.
    ''' </summary>
    ''' <param name="command">A string expression to evaluate</param>
    ''' <returns>A double with the result of the evaluated command parameter</returns>
    ''' <remarks>Vulnerable to injection attacks.</remarks>
    Private Function ProcessCommand(ByVal command As String) As Double
        Dim MyProvider As New VBCodeProvider 'Create a new VB Code Compiler
        Dim cp As New CompilerParameters     'Create a new Compiler parameter object.
        cp.GenerateExecutable = False        'Don't create an object on disk
        cp.GenerateInMemory = True           'But do create one in memory.
        'If cp.OutputAssembly is used with a VBCodeProvider, it seems to want to read before it is executed. 
        'See C# CodeBank example for explanation of why it was used.

        'the below is an empty VB.NET Project with a function that simply returns the value of our command parameter.
        Dim TempModuleSource As String = "Imports System" & Environment.NewLine & _
                                         "Namespace ns " & Environment.NewLine & _
                                         "Public Class class1" & Environment.NewLine & _
                                         "Public Shared Function Evaluate()" & Environment.NewLine & _
                                         "Return " & command & Environment.NewLine & _
                                         "End Function" & Environment.NewLine & _
                                         "End Class" & Environment.NewLine & _
                                         "End Namespace"
        'Create a compiler output results object and compile the source code.
        Dim cr As CompilerResults = MyProvider.CompileAssemblyFromSource(cp, TempModuleSource)
        If cr.Errors.Count > 0 Then
            'If the expression passed is invalid or "", the compiler will generate errors.
            Throw New ArgumentOutOfRangeException("Invalid Expression - please use something VB could evaluate")
        Else
            'Find our Evaluate method.
            Dim methInfo As MethodInfo = cr.CompiledAssembly.GetType("ns.class1").GetMethod("Evaluate")
            'Invoke it on nothing, so that we can get the return value
            Return Convert.ToDouble(methInfo.Invoke(Nothing, Nothing))
        End If
    End Function
Sample usage and output:

VB Code:
Code: [Select]
MsgBox(ProcessCommand(TextBox1.Text)) 'SHows a message box with the result of an expression in TextBox1
        Console.WriteLine(ProcessCommand("1+1").ToString()) 'Displays 2
        Console.WriteLine(ProcessCommand("Math.PI").ToString()) 'Displays 3.14159265358979
        Console.WriteLine(ProcessCommand("Math.Abs(-22)").ToString()) 'Displays 22
        Console.WriteLine(ProcessCommand("3-4+6+7+22/3+66*(55)").ToString()) 'Displays 3649.333333333333333333 




hugha

  • Newt
  • Posts: 103
Re: VB.NET Evaluation of a VB.net Expression
« Reply #12 on: May 29, 2010, 02:29:14 AM »
.NET doesn't provide an eval, but you can invoke vbscript:
http://www.devx.com/vb2themax/Tip/18773
http://weblogs.asp.net/rosherove/art...scripting.aspx

So following the example shown here
http://through-the-interface.typepad...g_started.html

In Visual Studio VB.NET:
  • create a project called VBMgdAcad1
  • add a reference to the COM Microsoft Script Control
  • then compile this VB.NET code :


Imports Autodesk.AutoCAD.Runtime

PublicClass CIMMClass
' Define command 'Asdkcmd1'
<CommandMethod("Asdkcmd1")> _
  PublicSub Asdkcmd1()
    MsgBox("Hello!" + myEval("12 + 3 * 10"))
  EndSub
  PublicFunction myEval(ByVal expr AsString) AsString
    Dim sc AsNew MSScriptControl.ScriptControl()
    sc.Language = "VBScript"
    myEval = sc.Eval(expr)
  EndFunction
EndClass


In ACAD:
  • NETLOAD VBMgdAcad1.dll
  • Issue Asdkcmd1 at ACAD's command prompt

to see Hello!42 in a messagebox.


hth
Hugh Adamson
www.hatchkit.com.au

hugha

  • Newt
  • Posts: 103
« Last Edit: June 01, 2010, 07:17:11 AM by hugha »

Peter Jamtgaard

  • Guest
Re: VB.NET Evaluation of a VB.net Expression
« Reply #14 on: May 31, 2010, 12:07:50 PM »
Quote
the assembly would then netload the code in a wrapper (we should be able to capture the wrapper and use it to unload it)

Keith,

Do you have an example or can you direct me or to an example where a routine captures a wrapper and uses it to unload it?

That would be REALLY useful.

Having a netunload expression would be good.

Peter