tags:

views:

32

answers:

1

Is it possible to overload how a type resolves properties when cast to dynamic?

Basically what I am trying to do is something like ExpandoObject where you can access new properties on the fly, but instead of those properties being whatever gets assigned to them, I want them all to be collections of strings, and when you access one, it calls a dictionary web service which returns all of the definitions of the word. I want it to be dynamic so that you can specify any word at all.

I'm thinking the usage would be something like this:

dynamic dictionary = new DictionaryWebservice();
var awesome = dictionary.awesome;

foreach(var definition in awesome)
{
    // add definition to my output
}

Is this possible?

+3  A: 

Yes, derive your type from System.Dynamic.DynamicObject.

Remember though that everything you return from a dynamic method is itself dynamic. Therefore, even the var definition in your foreach will be of type dynamic.

Timwi
Awesome! (5 more to go...)
NickLarsen
[Just link here next time you need a short comment ☺](http://meta.stackoverflow.com/questions/700/)
Timwi