C# Eval
  • Version:
  • 1.0
  • 1.1

Downloads
You can download the latest .dll from the Download section.
Adding to a project
Open a pre-existing or new project in Visual Studio. In Solution Explorer, right click on 'References' (from the root list not the one in the Content directory). Choose 'Add Reference' and click on the 'Browse' tab. Browse to where you downloaded the .dll, select it, and hit Ok.
How to Setup in Code
Add a using statement for the "CSE" namespace in your main cs file. Then follow one of these methods:

Method A:
Call CsEval.Eval, passing in the environment and the string to evaluate.
The environment is the specific instance from which expressions are evaluated in. e.g. passing in "this" in C# or "me" in VB will use the current class as the environment.

Sample Code:

	class SomeClass {
		...
		int count = 7;
		...

		void Calculate() {
			...
			CsEval.Eval(this, "3 + count / 2");
			...
		}

		...
	}
								

Method B:
  1. Use CsEval.EvalEnvironment to set the environment. This will store the evaluation environment, which will be used for all expression evaluations until it is set again.
  2. Call CsEval.Eval, passing in the string to evaluate.

Sample Code:

	class SomeClass {
		...
		int count = 7;
		...

		void Calculate() {
			...
			CsEval.EvalEnvironment = this;
			CsEval.Eval("3 + count / 2");
			...
		}

		...
	}
								

Method C:
  1. Create a CsEvaluator instance and pass it the returned value from CsEval.GetEvaluator(expression)
  2. Call CsEval.Eval, passing in the CsEvaluator.
This method is useful in the case that the expression to be evaluated doesn't change. The expression is only parsed a single time, and is stored within the CsEvaluator. Call CsEval.Eval on the CsEvaluator each time it is necessary to evaluate the expression.

Sample Code:

	class SomeClass {
		...
		int count = 7;
		...

		void Calculate() {
			...
			CsEvaluator evaluator = CsEval.GetEvaluator("3 + count / 2");
			CsEval.Eval(evaluator);
			...
		}

		...
	}
								

How to Use During Runtime
Expressions That are Allowed:
L = lhs expression in assignment, R = rhs expression in assignment or non-assignment expression
[R]  Arithmetic including a power operator (**)
[R]  Arithmetic operator equals (+=, -=, /=, *=, %=, and **=)
[LR]  Array indexing
[R]  Casting (Can only cast primitive struct types or any class that implements IConvertible)
[R]  Enums
[LR]  Identifiers (fields or properties)
[R]  Literals (numeric, char, string, and boolean). May also suffix any numeric literal with chars associated with C# types (i.e. "m" for decimal, "ul" for unsigned long, etc.)
[R]  Methods (void or single return)
[R]  New operator
[R]  Object chains (e.g. player.Position.X)
[R]  Types (for calling static fields, properties, and methods)
Expressions That are Not Yet Allowed:
Anonymous Types/Functions
Array initialization
Collection initialization
Conditional logic (including ternary statements)
Generics
Lambda Functions
LINQ
Logic Operators (&, |, ^, &&, ||)
Out and Ref operators
Property indexers
This pointer
Typeof, Default, As, and Is operators
CSE Constants
The following expressions are recognized as constants.
  • "#C":  299,792,458 (Speed of light)
  • "#E": Shorthand for Math.E
  • "#GAMMA": 0.5772156649 (Euler's constant)
  • "#G": 9.8 (Gravity)
  • "#GC": 6.673e-11 (Gravitational constant)
  • "#H": 6.62606893e-34 (Planck's constant)
  • "#PHI": (Math.sqrt(5)+1.0)/2 (Golden Ratio)
  • "#PI": Shorthand for Math.PI
Examples:
Given this code:
public class One {
public Two Two { get; set; }
public One() {
Two = new Two();
}
}

public class Two {
public Three Three { get; set; }
public Two() {
Three = new Three();
}
public byte Num() {
return 230;
}
}

public class Three {
public byte TheValue { get { return 100; } }
}

Some expressions could be:
new StringBuilder(((float)new One().Two.Num()).ToString() + ((float)new One().Two.Three.TheValue).ToString())
someIdentifier = new DateTime(new Random().Next(255), 10, new One().Two.Num() / 10)