Dynamically calculate the class of string expressions

xiaoxiao2021-03-05  32

Recently used this feature, to calculate some string expressions, such as "1 3/2", etc., write this class. The principle is very simple, that is, using the CodeDom dynamically generates an assembly containing a custom function, and then calls the reflection. The online example is very much. I can use this class, you can use it directly, you can use it directly, don't read the code. Support common mathematics functions, such as SiN, Log, etc., and do not vase.

Using

System;

Using

System.collections.Generic;

Using

System.Text;

Using

System.codedom.compiler;

Using

Microsoft.visualbasic;

Using

System.Reflection;

Namespace

Ninputer.utilities

...

{

Sealed Class Evaluator

... {

Private static codeDomprovider comp = new vbcodeprovider ();

Private static compilerparameters cp = new compilerparameters ();

Private static methodinfo mi;

Public Static Object Eval (String Expression)

... {

Stringbuilder CodeBuilder = new stringbuilder ();

CodeBuilder.Appendline ("imports system");

CodeBuilder.Appendline ("Imports System.math");

CodeBuilder.Appendline ("Imports Microsoft.visualBasic");

CodeBuilder.Appendline ();

CodeBuilder.Appendline ("Public Module Mode");

CodeBuilder.Appendline ("Public Function func () as object");

CodeBuilder.Appendline ("Return" Expression);

CodeBuilder.Appendline ("End Function");

CodeBuilder.Appendline ("end module");

cp.referencedassemblies.add ("system.dll");

cp.referencedassemblies.add ("Microsoft.VisualBasic.dll");

Cp.generateexecutable = false;

Cp.GenerateInmemory = True;

String code = codebuilder.toString ();

CompilerResults cr = comp.compileAssemblyfromsource (CP, CODE);

IF (cr.erroS.haserror)

... {Return Null;}

Else

... {askDAMBLY A = cr.compiledassembly; type t = a.gettype ("mode"); // Object mode = a.createInstance ("MODE"); mi = T. GetMethod ("func", bindingflags.static | Bindingflags.public; return mi.invoke (null, new object [0]);}}

}

}

Usage is also very simple, in fact, in addition to mathematical expressions, any one-on-surface effective VB expression can be implemented.

Class

PROGRAM

... {

Static void main (string [] args)

... {

Double a = (double) Evaluator.eval ("SIN (0.2) 5");

Console.writeline (a.tostring ());

}

}

Note that I use the syntax of .NET 2.0, use .NET 1.1 / vs.net 2003, please use this code:

Using system;

Using system.text;

Using system.codedom.compiler;

USING Microsoft.visualBasic;

Using system.reflection;

Namespace ninputer.utilities

{

Sealed Class Evaluator

{

Private static codeDomprovider comp = new vbcodeprovider ();

Private static compilerparameters cp = new compilerparameters ();

Private static methodinfo mi;

Public Static Object Eval (String Expression)

{

Stringbuilder CodeBuilder = new stringbuilder ();

CodeBuilder.Appendline ("imports system");

CodeBuilder.Appendline ("Imports System.math");

CodeBuilder.Appendline ("Imports Microsoft.visualBasic");

CodeBuilder.Appendline ();

CodeBuilder.Appendline ("Public Module Mode");

CodeBuilder.Appendline ("Public Function func () as object");

CodeBuilder.Appendline ("Return" Expression);

CodeBuilder.Appendline ("End Function");

CodeBuilder.Appendline ("end module");

cp.referencedassemblies.add ("system.dll");

cp.referencedassemblies.add ("Microsoft.VisualBasic.dll");

Cp.generateexecutable = false;

Cp.GenerateInmemory = True;

String code = codebuilder.toString ();

CompilerResults cr = Comp.createCompiler (). CompileAssemblyfromsource (CP, CODE);

IF (cr.erroS.haserror)

{

Return NULL;

}

Else

{

Assembly a = cr.compiledassembly;

TYPE T = a.gettype ("Mode");

// Object Mode = a.createInstance ("Mode");

Mi = T. GetMethod ("func", bindingflags.static | bindingflags.public);

Return Mi.invoke (Null, New Object [0]);

}

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-34074.html

New Post(0)