Click here to Skip to main content
15,298,916 members
Articles / Programming Languages / C#
Posted 6 Mar 2006


49 bookmarked

C# Eval Function

Rate me:
Please Sign up or sign in to vote.
3.58/5 (20 votes)
21 Jun 2007CPOL
C# eval function


This article is about a C# eval function, how to parse an expression and evaluate it. It supports Boolean, Bitwise, Arithmetic, Unary, Paren, Member (such as, Indexer (such as array[index]) and ConditionalIf(?:), it also supports Variable assign.

You can use it to evaluate one statement in the C# source code.

It does not use runtime compiler.

It does not use JScript.NET.

It does not use DataTable.

Using the Code

Expression Example



All functions or properties use Reflection to eval. It has no global function and its syntax is checked at runtime.
If you let Parameter 'string'=typeof(string), then it can do like this:


If a parameter is a type, it will call its static function or property.





Sample Use

using System;
using Jyc.Expr;
namespace TestExpr
    class Program
        static void Main(string[] args)
            Parser ep = new Parser();
            Evaluater evaluater = new Evaluater();
            ParameterVariableHolder pvh = new ParameterVariableHolder();

            pvh.Parameters["char"] = new Parameter(typeof(char));
            pvh.Parameters["sbyte"] = new Parameter(typeof(sbyte));
            pvh.Parameters["byte"] = new Parameter(typeof(byte));
            pvh.Parameters["short"] = new Parameter(typeof(short));
            pvh.Parameters["ushort"] = new Parameter(typeof(ushort));
            pvh.Parameters["int"] = new Parameter(typeof(int));
            pvh.Parameters["uint"] = new Parameter(typeof(uint));
            pvh.Parameters["long"] = new Parameter(typeof(string));
            pvh.Parameters["ulong"] = new Parameter(typeof(ulong));
            pvh.Parameters["float"] = new Parameter(typeof(float));
            pvh.Parameters["double"] = new Parameter(typeof(double));
            pvh.Parameters["decimal"] = new Parameter(typeof(decimal));
            pvh.Parameters["DateTime"] = new Parameter(typeof(DateTime));
            pvh.Parameters["string"] = new Parameter(typeof(string));

            pvh.Parameters["Guid"] = new Parameter(typeof(Guid));

            pvh.Parameters["Convert"] = new Parameter(typeof(Convert));
            pvh.Parameters["Math"] = new Parameter(typeof(Math));
            pvh.Parameters["Array "] = new Parameter(typeof(Array));
            pvh.Parameters["Random"] = new Parameter(typeof(Random));
            pvh.Parameters["TimeZone"] = new Parameter(typeof(TimeZone));
            pvh.Parameters["AppDomain "] = new Parameter(typeof(AppDomain));

            pvh.Parameters["Console"] = new Parameter(typeof(Console));

            pvh.Parameters["evaluater"] = new Parameter(evaluater);

            evaluater.VariableHolder = pvh;

            while (true)
                System.Console.WriteLine("Input line,press Return to Eval:");
                string line = System.Console.ReadLine().Trim();
                if (string.IsNullOrEmpty(line))
                    Tree tree = ep.Parse(line);


                    object result = evaluater.Eval(tree);

                    if( result != null )
                        System.Console.WriteLine("Result:{0}", result);
                        System.Console.WriteLine("Result is null");
                catch (Exception e)
                    System.Console.WriteLine("Exception:" + e.GetType().Name _
                                               +"->"+ e.Message);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
China China
No Biography provided

Comments and Discussions

QuestionBug in Scanner.cs, at line 453 Pin
Member 864646714-Feb-12 7:42
MemberMember 864646714-Feb-12 7:42 
QuestionThanks Pin
bildik9-Aug-11 10:58
Memberbildik9-Aug-11 10:58 
GeneralMy vote of 5 Pin
Chen Noam4-Oct-10 21:17
MemberChen Noam4-Oct-10 21:17 
GeneralGracias! Pin
Member 44952424-Jun-10 6:00
MemberMember 44952424-Jun-10 6:00 
Generalthanks! Pin
bpbrainiak4-May-10 4:00
Memberbpbrainiak4-May-10 4:00 
GeneralRounding problem Pin
Pauline Cole30-Nov-09 11:20
MemberPauline Cole30-Nov-09 11:20 
GeneralCalculation bug with negative numbers Pin
Pauline Cole11-Oct-09 23:51
MemberPauline Cole11-Oct-09 23:51 
GeneralRe: Calculation bug with negative numbers Pin
bpbrainiak4-May-10 3:56
Memberbpbrainiak4-May-10 3:56 
GeneralNice Pin
KeitaroSan8-Oct-09 7:17
MemberKeitaroSan8-Oct-09 7:17 
GeneralDon't use it for work, only for learning stuff Pin
Dannie Juge30-Sep-09 3:25
MemberDannie Juge30-Sep-09 3:25 
QuestionParse as decimal values Pin
palswim15-Jun-09 11:35
Memberpalswim15-Jun-09 11:35 
AnswerRe: Parse as decimal values Pin
palswim15-Jun-09 11:37
Memberpalswim15-Jun-09 11:37 
GeneralLicense Pin
Member 256327611-May-09 3:54
MemberMember 256327611-May-09 3:54 
GeneralRe: License Pin
devilplusplus30-May-09 15:30
Memberdevilplusplus30-May-09 15:30 
GeneralOne slight correction in the test code Pin
Andrew Kaiser26-Nov-08 2:26
MemberAndrew Kaiser26-Nov-08 2:26 
GeneralCalculation Error Pin
pauf15-Nov-08 0:42
Memberpauf15-Nov-08 0:42 
QuestionAssign Variables Pin
j_jpg12-Nov-08 0:54
Memberj_jpg12-Nov-08 0:54 
GeneralGreat work Pin
Chen Noam5-Nov-08 22:44
MemberChen Noam5-Nov-08 22:44 
Generalcan't parse Pin
jameswei1-Oct-08 16:43
Memberjameswei1-Oct-08 16:43 
AnswerRe: can't parse Pin
Chen Noam5-Nov-08 22:47
MemberChen Noam5-Nov-08 22:47 
Questionis it possible to add variable in runtime ? Pin
naamakat25-May-08 2:15
Membernaamakat25-May-08 2:15 
AnswerRe: is it possible to add variable in runtime ? Pin
Member 110949439-Oct-14 3:06
MemberMember 110949439-Oct-14 3:06 
GeneralGetting data from a deep object Pin
ftuttle13-May-08 7:05
Memberftuttle13-May-08 7:05 
QuestionLicense Pin
Kjell Ahlstrom2-Mar-08 22:10
MemberKjell Ahlstrom2-Mar-08 22:10 
GeneralRe: License Pin
devilplusplus8-Apr-08 14:09
Memberdevilplusplus8-Apr-08 14:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.