Create a gist now

Instantly share code, notes, and snippets.

Embed
IronPython and C# 4 "dynamic"
using System;
using IronPython.Hosting;
public class dynamic_demo {
static void Main() {
var ipy = Python.CreateRuntime();
dynamic mock = ipy.UseFile("mock.py");
// Calls the Python Mock type's constructor
dynamic m = mock.Mock();
//The Python Mock type dynamically implements any member that is requested of it
System.Console.WriteLine(
m.the_csharp_compiler_cannot_possbily_know_this_member_exists_at_compile_time
);
}
}
import random, math
class Mock(object):
def __getattr__(self, key):
"""Mock objects of this type will dynamically implement any requested member"""
return random.choice(["hello world", math.pi])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment