Statistics
| Branch: | Revision:

root / trunk / Pithos.Core / DynamicDictionary.cs @ 73cdd135

History | View | Annotate | Download (1.6 kB)

1 9c4346c9 Panagiotis Kanavos
using System.Collections.Generic;
2 9c4346c9 Panagiotis Kanavos
using System.Dynamic;
3 9c4346c9 Panagiotis Kanavos
4 9c4346c9 Panagiotis Kanavos
namespace Pithos.Core
5 9c4346c9 Panagiotis Kanavos
{
6 9c4346c9 Panagiotis Kanavos
    public class DynamicDictionary : DynamicObject
7 9c4346c9 Panagiotis Kanavos
    {
8 9c4346c9 Panagiotis Kanavos
        // The inner dictionary.
9 9c4346c9 Panagiotis Kanavos
        Dictionary<string, object> dictionary
10 9c4346c9 Panagiotis Kanavos
            = new Dictionary<string, object>();
11 9c4346c9 Panagiotis Kanavos
12 9c4346c9 Panagiotis Kanavos
        // This property returns the number of elements
13 9c4346c9 Panagiotis Kanavos
        // in the inner dictionary.
14 9c4346c9 Panagiotis Kanavos
        public int Count
15 9c4346c9 Panagiotis Kanavos
        {
16 9c4346c9 Panagiotis Kanavos
            get
17 9c4346c9 Panagiotis Kanavos
            {
18 9c4346c9 Panagiotis Kanavos
                return dictionary.Count;
19 9c4346c9 Panagiotis Kanavos
            }
20 9c4346c9 Panagiotis Kanavos
        }
21 9c4346c9 Panagiotis Kanavos
22 9c4346c9 Panagiotis Kanavos
        // If you try to get a value of a property 
23 9c4346c9 Panagiotis Kanavos
        // not defined in the class, this method is called.
24 9c4346c9 Panagiotis Kanavos
        public override bool TryGetMember(
25 9c4346c9 Panagiotis Kanavos
            GetMemberBinder binder, out object result)
26 9c4346c9 Panagiotis Kanavos
        {
27 9c4346c9 Panagiotis Kanavos
            // Converting the property name to lowercase
28 9c4346c9 Panagiotis Kanavos
            // so that property names become case-insensitive.
29 9c4346c9 Panagiotis Kanavos
            string name = binder.Name.ToLower();
30 9c4346c9 Panagiotis Kanavos
31 9c4346c9 Panagiotis Kanavos
            // If the property name is found in a dictionary,
32 9c4346c9 Panagiotis Kanavos
            // set the result parameter to the property value and return true.
33 9c4346c9 Panagiotis Kanavos
            // Otherwise, return false.
34 9c4346c9 Panagiotis Kanavos
            return dictionary.TryGetValue(name, out result);
35 9c4346c9 Panagiotis Kanavos
        }
36 9c4346c9 Panagiotis Kanavos
37 9c4346c9 Panagiotis Kanavos
        // If you try to set a value of a property that is
38 9c4346c9 Panagiotis Kanavos
        // not defined in the class, this method is called.
39 9c4346c9 Panagiotis Kanavos
        public override bool TrySetMember(
40 9c4346c9 Panagiotis Kanavos
            SetMemberBinder binder, object value)
41 9c4346c9 Panagiotis Kanavos
        {
42 9c4346c9 Panagiotis Kanavos
            // Converting the property name to lowercase
43 9c4346c9 Panagiotis Kanavos
            // so that property names become case-insensitive.
44 9c4346c9 Panagiotis Kanavos
            dictionary[binder.Name.ToLower()] = value;
45 9c4346c9 Panagiotis Kanavos
46 9c4346c9 Panagiotis Kanavos
            // You can always add a value to a dictionary,
47 9c4346c9 Panagiotis Kanavos
            // so this method always returns true.
48 9c4346c9 Panagiotis Kanavos
            return true;
49 9c4346c9 Panagiotis Kanavos
        }
50 9c4346c9 Panagiotis Kanavos
    }
51 9c4346c9 Panagiotis Kanavos
}