Skip to content

Instantly share code, notes, and snippets.

@hfoffani
Created August 25, 2016 09:53
Show Gist options
  • Select an option

  • Save hfoffani/48227a39971e91e427e77caa1e322c3d to your computer and use it in GitHub Desktop.

Select an option

Save hfoffani/48227a39971e91e427e77caa1e322c3d to your computer and use it in GitHub Desktop.
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe test1064.cs /reference:"c:\Program Files (x86)\IronPython 2.7\IronPython.dll" /reference:"c:\Program Files (x86)\IronPython 2.7\Microsoft.Scripting.dll"
test1064.exe
using System.Dynamic;
using System.Linq.Expressions;
using System;
using IronPython.Hosting;
public class Dyn : DynamicObject
{
private readonly string text;
public Dyn(string text)
{
this.text = text;
}
public override string ToString()
{
return this.text;
}
public override bool TryBinaryOperation(BinaryOperationBinder binder, object arg, out object result)
{
result = new Dyn(this + " " + binder.Operation + " " + arg);
return true;
}
public override bool TryUnaryOperation(UnaryOperationBinder binder, out object result)
{
switch (binder.Operation)
{
case ExpressionType.IsFalse:
case ExpressionType.IsTrue:
result = false;
return true;
}
return base.TryUnaryOperation(binder, out result);
}
}
class Program
{
static void Main(string[] args)
{
dynamic a = new Dyn("a");
dynamic b = new Dyn("b");
dynamic c = new Dyn("c");
var correct = a && b || c;
var engine = Python.CreateEngine();
var scope = engine.CreateScope();
scope.SetVariable("a", a);
scope.SetVariable("b", b);
scope.SetVariable("c", c);
var incorrect = engine.Execute("a and b or c", scope);
Console.WriteLine("Correct: " + correct);
Console.WriteLine("Incorrect: " + incorrect);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment