[Mono-list] DynamicObject

Ben Timby btimby at gmail.com
Fri Feb 22 19:45:10 UTC 2013


This question is partially about C# itself, and partially about the
mono framework.

I have some Python code I am trying to port to C#. Specifically, I am
using dynamic members (attributes in Python). They are created upon
being accessed.

obj = MyClass()
obj.dynamic_attribute_one = 1
obj.dynamic_attribute_two = 2

This is done in python using __getattr__() and __setattr__() magic
methods. I think C# can do something similar using the DynamicObject
and it's TryMember() method.

http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.aspx

What I am actually doing is writing a REST API client, the client
library allows the caller to do something like:

client.objects.subtype.get(1234)

Which results in a `GET /objects/subtype/1234/` request. This works
very well in Python, and I am trying to emulate this in C#. The main
requirement is that the client library does not need to know what
endpoints exist, I want members to be "created" when accessed, not
pre-defined in the class definition.

I am unsure if DynamicObject is available under Mono, or if there is
another way to accomplish what I am looking for.


More information about the Mono-list mailing list