45

I'm trying to understand the DynamicObject type. Found this MSDN article to be very consise and clear as how to create and use DynamicObject:

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

The article contains a simple DynamicDictionary class that inherits from DynamicObject.

Now I want to iterate over my dynamically created DynamicObject properties:

dynamic d = new DynamicDictionary();
d.Name = "Myname";
d.Number = 1080;

foreach (var prop in d.GetType().GetProperties())
{
  Console.Write prop.Key;
  Console.Write prop.Value;
}

Obviously that does not work. I want to learn how to do this without a change in my DynamicDictionary class, since I'm really trying to learn how to use this for all kinds of existing objects that inherits from DynamicObject.

Is Reflection needed? I must be missing something...

joeriks
  • 3,382
  • 8
  • 32
  • 42

3 Answers3

51

I believe you'd be interested in the ExpandoObject class. The DynamicObject class is just a base where you're meant to provide all the logic. It explicitly implements the IDictionary<string, object> interface so you can access it properties or add new ones that way.

// declare the ExpandoObject
dynamic expObj = new ExpandoObject();
expObj.Name = "MyName";
expObj.Number = 1000;

// print the dynamically added properties
foreach (KeyValuePair<string, object> kvp in expObj) // enumerating over it exposes the Properties and Values as a KeyValuePair
    Console.WriteLine("{0} = {1}", kvp.Key, kvp.Value);

// cast to an IDictionary<string, object>
IDictionary<string, object> expDict = expObj;

// add a new property via the dictionary reference
expDict["Foo"] = "Bar";

// verify it's been added on the original dynamic reference
Console.WriteLine(expObj.Foo);

I had just realized that you are implementing the DynamicDictionary class and misunderstood your question. Sorry.

When used with a dynamic reference, you only have access to publicly exposed members. Since only Count is declared public (besides the other DynamicObject members), you'd need to use reflection in order to access the inner dictionary to easily get those values (if you don't intend to make any further changes).

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
  • Ok, thanks, so - for an existing object which inherits DynamicObject and does not expose the dictionary - there's no way for me (other than to use Reflection) to get the members dictionary? – joeriks Jan 27 '11 at 09:41
  • AFAIK yes. You're only able to access public members and the `DynamicObject` class where member access is resolved through the various methods. Alternatively, you could explicitly implement the `IDictionary` class just like the ExpandoObject to make this process easier. – Jeff Mercado Jan 27 '11 at 09:58
  • p.s., Sorry I glossed over your question at first. I didn't notice that you were actually implementing the example code. I was thinking to myself, "There's no `DynamicDictionary` class..." :) – Jeff Mercado Jan 27 '11 at 10:02
  • The part of the answer that does not answer the question is exactly what I was looking for. – Bovaz Sep 22 '16 at 13:29
14

Did you tried DynamicDictionary.GetDynamicMemberNames() method? - http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.getdynamicmembernames.aspx

Anuraj
  • 18,859
  • 7
  • 53
  • 79
  • Looks good - but I dont want to / cannot change my class and "GetDynamicMemberNames() is an optional thing, i.e. a class inheriting from DynamicObject can override it if it wants, but if it doesn't the function in the base class gets called which returns an empty list." – joeriks Jan 27 '11 at 09:57
  • Hmm I understand. Then I think either you need to go for ExpandoObject or you need it implement it your own. – Anuraj Jan 27 '11 at 10:15
0

I know this topic is pretty old but for those who are still wondering on how to do it, why not use the TryGetMember() method but construct a class that inherits the GetMemberBinder, like:

public class MyGetMemberBinder : GetMemberBinder
{
    public MyGetMemberBinder(string name, bool ignorecase) : base(name, ignorecase) { }
    public override DynamicMetaObject FallbackGetMember(DynamicMetaObject target, DynamicMetaObject errorSuggestion)=>throw new NotImplementedException();
}

Then with LINQ:

Dictionary<string, object> AsStandardDictionary = DynamicDictionary.GetDynamicMemberNames().ToDictionary(x => x, y =>
{
    if(DynamicDictionary.TryGetMember(new MyGetMemberBinder(y, false), out var result))
        return result;
    else
        return null;
});
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
IRLotsEvil
  • 36
  • 2