OkEval 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package OkEval --version 1.0.0                
NuGet\Install-Package OkEval -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OkEval" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OkEval --version 1.0.0                
#r "nuget: OkEval, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install OkEval as a Cake Addin
#addin nuget:?package=OkEval&version=1.0.0

// Install OkEval as a Cake Tool
#tool nuget:?package=OkEval&version=1.0.0                
// Easy to use
int result = Eval.Execute<int>("X + Y", new { X = 1, Y = 2});

//Execute C# Code
var list = new List<int>() { 1, 2, 3, 4 };
var greaterThan = 2;
list = Eval.Execute<List<int>>("list.Where(x => x > greaterThan)", 
   new { list, greaterThan });

//Compile C# Code
var compiled = Eval.Compile<Func<List<int>>>(@"
var list = new List<int>() { 1, 2, 3, 4 };
return list.Where(x => x > 2).ToList();
");
var list = compiled();

//LINQ Dynamic
var dict = new Dictionary<string, object>();
dict.Add("Statut", 0);
dict.Add("LastLogon", DateTime.Now.AddMonths(-1));
 
var customers = context.Customers
 .WhereDynamic(x => "x.Status == Statut 
                     && x.LastLogon >= LastLogon", dict)
 .ToList();

var x = "1+2".Execute<int>(); // return 3

int result = Eval.Execute<int>("X + Y", new { X = 1, Y = 2});

int result = Eval.Execute<int>(@"
    var list = new List<int>() { 1, 2, 3, 4, 5 };
    var filter = list.Where(x => x < 4);
    return filter.Sum(x => x);");

// NOTE: The list returned contains "3" and "4"
var list = Eval.Execute(@"
var list = new List<int>() { 1, 2, 3, 4 };
return list.Where(x => x > 2).ToList();
");

var list = new List<int>() { 1, 2, 3, 4 };
var greaterThan = 2;

// Passing parameters with an Anonymous Type
{
	var rList = Eval.Execute("list.Where(x => x > greaterThan)", new { list, greaterThan });
}		

// Passing parameters with an Anonymous Type using named members
{
	var rList = Eval.Execute("listName.Where(x => x > greaterThanName)", new { listName = list, greaterThanName = greaterThan });
}		

// Passing parameters with a class instance and using class members
// NOTE: When only using 1 parameter, you can directly use member names, "Item1" and "Item2" in the case of a Tuple<,>
{
	var rList = Eval.Execute("Item1.Where(x => x > Item2)", new Tuple<List<int>, int>(list, greaterThan));
}		

// Passing parameters with a Dictionary and using key names
// NOTE: When only using 1 parameter, you can directly use dictionary key names in the expression
{
	var dictionary = new Dictionary<string, object>();
	dictionary.Add("list", list);
	dictionary.Add("greaterThan", greaterThan);
	var rList = Eval.Execute("list.Where(x => x > greaterThan)", dictionary);
}

// Passing parameters using an Expando Object and using member names
// NOTE: When only using 1 parameter, you can directly use member names of the Expando Object
{
	dynamic expandoObject = new ExpandoObject();
	expandoObject.list = list;
	expandoObject.greaterThan= greaterThan;
	var rList = Eval.Execute("list.Where(x => x > greaterThan)", expandoObject);
}

// Passing parameters directly with the Values
// NOTE: Because parameters are not named, you need to use the position as our library is not aware of the name "list" and "greaterThan"
{
	var rList = Eval.Execute("{0}.Where(x => x > {1})", list, greaterThan);
}

//Evaluate a C# expression with a return type
var list = new List<int>() { 1, 2, 3, 4 };
var greaterThan = 2;

list = Eval.Execute<List<int>>("list.Where(x => x > greaterThan)", new { list, greaterThan });

//Evaluate a C# expression from a string
var list = new List<int>() { 1, 2, 3, 4 };
var greaterThan = 2;

var expressionToExecute = "list.Where(x => x > greaterThan)";
list = expressionToExecute.Execute<List<int>>(new { list, greaterThan });

// SIMILAR TO: list = Eval.Execute<List<int>>("list.Where(x => x > greaterThan)", new { list, greaterThan });

//Evaluate a C# expression from a context
var list = new List<int>() { 1, 2, 3, 4 };
var greaterThan = 2;

var context = new EvalContext();

context.AddMethod(@"
bool GreaterThan(this int x, int y)
{
	return x > y;
}
");

list = context.Execute<List<int>>("list.Where(x => x.GreaterThan(greaterThan))", new { list, greaterThan });

//Compile a C# expression
// NOTE: The returned list contains "3" and "4"
var compiled = Eval.Compile<Func<List<int>>>(@"
var list = new List<int>() { 1, 2, 3, 4 };
return list.Where(x => x > 2).ToList();
");

var list = compiled();

//Compile a C# expression with return type
// NOTE: The returned list contains "3" and "4"
var compiled = Eval.Compile<Func<List<int>>>(@"
var list = new List<int>() { 1, 2, 3, 4 };
return list.Where(x => x > 2).ToList();
");

var list = compiled();

//Compile a C# expression with variable names
var list = new List<int>() { 1, 2, 3, 4 };
var greaterThan = 2;

// Passing parameters with position
// NOTE: In this example, {0} is the first `List<int>` and {1} is the `int`
{
	var compiled = Eval.Compile<Func<List<int>, int, List<int>>>("{0}.Where(x => x > {1})");
	var rList = compiled(list, greaterThan);
	FiddleHelper.WriteTable((IEnumerable<int>)rList);
}

// Passing parameters with named variables
// NOTE: You need to name as many variables as the `Func` have (without counting the return type)
{
	var compiled = Eval.Compile<Func<List<int>, int, List<int>>>("list.Where(x => x > greaterThan)", "list", "greaterThan");
	var rList = compiled(list, greaterThan);
	FiddleHelper.WriteTable((IEnumerable<int>)rList);
}

// Passing parameters with an ExpandoObject
// NOTE: More flexible than the 2 previous examples but less robust as the library assume any missing name is part of the ExpandoObject
// NOTE: We had to cast the list to `(List<int>)` as until the method is executed, the library doesn't know which type is the variable `list`
{
	dynamic expandoObject = new ExpandoObject();
	expandoObject.list = list;
	expandoObject.greaterThan= greaterThan;
	
	var compiled = Eval.Compile<Func<ExpandoObject, List<int>>>("((List<int>)list).Where(x => x > greaterThan)");
	var rList = compiled(expandoObject);
	FiddleHelper.WriteTable((IEnumerable<int>)rList);
}

//Compile a C# expression from a string
var list = new List<int>() { 1, 2, 3, 4 };

var expression = "list.Where(x => x > i)";

var compiled = expression.Compile<Func<List<int>, int, List<int>>>("list", "i");
// SIMILAR TO: list = Eval.Compile<Func<List<int>, int, List<int>>>(expression, "list", "i");		

for(int i = 0; i < 4; i++)
{
	var rList = compiled(list, i);
	FiddleHelper.WriteTable("i=" + i, rList);
}

//Compile a C# expression from a context
var list = new List<int>() { 1, 2, 3, 4 };		
var context = new EvalContext();

context.AddMethod(@"
bool GreaterThan(this int x, int y)
{
	return x > y;
}
");

var compiled = context.Compile<Func<List<int>, int, List<int>>>("list.Where(x => x.GreaterThan(i))", "list", "i");	

for(int i = 0; i < 4; i++)
{
	var rList = compiled(list, i);
	FiddleHelper.WriteTable("i=" + i, rList);
}
class Calculator
{
	public static int Add(int a, int b)
	{
		return a + b;
	}

	public static int Subtract(int a, int b)
	{
		return a - b;
	}
	public static int Multiply(int a, int b)
	{
		return a * b;
	}
	public static int Divide(int a, int b)
	{
		return a / b;
	}
}

public static void Example1()
{
	var context = new EvalContext();

	context.RegisterType(typeof(Calculator));

	List<string> expressions = new List<string>()
	{
		"Calculator.Add(10, 5)",
		"Calculator.Subtract(10, 5)",
		"Calculator.Multiply(10, 5)",
		"Calculator.Divide(10, 5)"
	};

    foreach (var expression in expressions)
    {
		var result = context.Execute<int>(expression);

		Console.WriteLine("{0}: {1}", expression, result);
	}
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Framework net40 is compatible.  net403 was computed.  net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 91 12/20/2024
1.0.0 187 5/26/2023