Iridium Script

Iridium-Script is a lightweight portable .NET library for expression evaluation and scripting

There are no dependencies and it will run on any .NET platform, including iOS and Android (using Xamarin). The expression syntax is compatible with C# 2.0 (so no support for generics)

Features

  • Evaluate any C# 2.0 expression at runtime
  • Application-specific data and types can be used in expressions
  • Support for scripting, including if/else, foreach, functions and macros.
  • Relaxed evaluation mode similar to JavaScript (falsy statements, etc.)
  • Render templates with embedded script code

Features at a glance

Expression evaluation

var context = new CSharpContext();

context["SomeString"] = "Hi there!";
context["SomeNumber"] = 20;

var parser = new CSharpParser();

var result = parser.Evaluate<int>("SomeNumber + SomeString.Length", context);  
// returns 29
Inject types and custom methods:
var context = new CSharpContext();

context.AddType("Math", typeof(Math));
context.Set("SomeString", "Hi there!");
context.Set("SomeNumber", 20);
context.AddFunction("fmt",  typeof(String), "Format");

var parser = new CSharpParser();

string stringValue = parser.Evaluate<string>("fmt(\"I said: {0}\", SomeString)", context);  
// returns "I said: Hi there!"

int intValue = parser.Evaluate<int>("Math.Max(10, SomeNumber)" , context); 
// returns 20

double doubleValue = parser.Evaluate<double>("SomeNumber * 2.0", context); 
// returns 40.0

Template rendering

// create parser using double curly braces syntax
var parser = new TemplateParser<DoubleCurly>(); 

var data = new { name = "John", age = 25 };

var s = parser.Render("My name is {{name}} and I'm {{age}} years old", data);

// s == "My name is John and I'm 25 years old"