Skip to content

Instantly share code, notes, and snippets.

@toptensoftware
Created October 26, 2016 03:52
Show Gist options
  • Select an option

  • Save toptensoftware/df13f8ee47d443ba503ce91d8b27e797 to your computer and use it in GitHub Desktop.

Select an option

Save toptensoftware/df13f8ee47d443ba503ce91d8b27e797 to your computer and use it in GitHub Desktop.
class MyLibrary
{
public MyLibrary()
{
pi = Math.PI;
}
public double pi { get; private set; }
public double r { get; set; }
public double rectArea(double width, double height)
{
return width * height;
}
public double rectPerimeter(double width, double height)
{
return (width + height) * 2;
}
}
[TestMethod]
public void Reflection()
{
// Create a library of helper function
var lib = new MyLibrary();
lib.r = 10;
// Create a context that uses the library
var ctx = new ReflectionContext(lib);
// Test
Assert.AreEqual(Parser.Parse("rectArea(10,20)").Eval(ctx), 200);
Assert.AreEqual(Parser.Parse("rectPerimeter(10,20)").Eval(ctx), 60);
Assert.AreEqual(Parser.Parse("2 * pi * r").Eval(ctx), 2 * Math.PI * 10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment