Created
October 22, 2009 08:03
-
-
Save jschementi/215819 to your computer and use it in GitHub Desktop.
IronPython and C# 4 "dynamic"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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