root / trunk / Pithos.Core / DynamicDictionary.cs @ dccd340f
History | View | Annotate | Download (3.4 kB)
1 | 255f5f86 | Panagiotis Kanavos | #region |
---|---|---|---|
2 | 255f5f86 | Panagiotis Kanavos | /* ----------------------------------------------------------------------- |
3 | 255f5f86 | Panagiotis Kanavos | * <copyright file="DynamicDictionary.cs" company="GRNet"> |
4 | 255f5f86 | Panagiotis Kanavos | * |
5 | 255f5f86 | Panagiotis Kanavos | * Copyright 2011-2012 GRNET S.A. All rights reserved. |
6 | 255f5f86 | Panagiotis Kanavos | * |
7 | 255f5f86 | Panagiotis Kanavos | * Redistribution and use in source and binary forms, with or |
8 | 255f5f86 | Panagiotis Kanavos | * without modification, are permitted provided that the following |
9 | 255f5f86 | Panagiotis Kanavos | * conditions are met: |
10 | 255f5f86 | Panagiotis Kanavos | * |
11 | 255f5f86 | Panagiotis Kanavos | * 1. Redistributions of source code must retain the above |
12 | 255f5f86 | Panagiotis Kanavos | * copyright notice, this list of conditions and the following |
13 | 255f5f86 | Panagiotis Kanavos | * disclaimer. |
14 | 255f5f86 | Panagiotis Kanavos | * |
15 | 255f5f86 | Panagiotis Kanavos | * 2. Redistributions in binary form must reproduce the above |
16 | 255f5f86 | Panagiotis Kanavos | * copyright notice, this list of conditions and the following |
17 | 255f5f86 | Panagiotis Kanavos | * disclaimer in the documentation and/or other materials |
18 | 255f5f86 | Panagiotis Kanavos | * provided with the distribution. |
19 | 255f5f86 | Panagiotis Kanavos | * |
20 | 255f5f86 | Panagiotis Kanavos | * |
21 | 255f5f86 | Panagiotis Kanavos | * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
22 | 255f5f86 | Panagiotis Kanavos | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 | 255f5f86 | Panagiotis Kanavos | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
24 | 255f5f86 | Panagiotis Kanavos | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
25 | 255f5f86 | Panagiotis Kanavos | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 | 255f5f86 | Panagiotis Kanavos | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 | 255f5f86 | Panagiotis Kanavos | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
28 | 255f5f86 | Panagiotis Kanavos | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
29 | 255f5f86 | Panagiotis Kanavos | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 | 255f5f86 | Panagiotis Kanavos | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
31 | 255f5f86 | Panagiotis Kanavos | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
32 | 255f5f86 | Panagiotis Kanavos | * POSSIBILITY OF SUCH DAMAGE. |
33 | 255f5f86 | Panagiotis Kanavos | * |
34 | 255f5f86 | Panagiotis Kanavos | * The views and conclusions contained in the software and |
35 | 255f5f86 | Panagiotis Kanavos | * documentation are those of the authors and should not be |
36 | 255f5f86 | Panagiotis Kanavos | * interpreted as representing official policies, either expressed |
37 | 255f5f86 | Panagiotis Kanavos | * or implied, of GRNET S.A. |
38 | 255f5f86 | Panagiotis Kanavos | * </copyright> |
39 | 255f5f86 | Panagiotis Kanavos | * ----------------------------------------------------------------------- |
40 | 255f5f86 | Panagiotis Kanavos | */ |
41 | 255f5f86 | Panagiotis Kanavos | #endregion |
42 | 9c4346c9 | Panagiotis Kanavos | using System.Collections.Generic; |
43 | 9c4346c9 | Panagiotis Kanavos | using System.Dynamic; |
44 | 9c4346c9 | Panagiotis Kanavos | |
45 | 9c4346c9 | Panagiotis Kanavos | namespace Pithos.Core |
46 | 9c4346c9 | Panagiotis Kanavos | { |
47 | 9c4346c9 | Panagiotis Kanavos | public class DynamicDictionary : DynamicObject |
48 | 9c4346c9 | Panagiotis Kanavos | { |
49 | 9c4346c9 | Panagiotis Kanavos | // The inner dictionary. |
50 | 9c4346c9 | Panagiotis Kanavos | Dictionary<string, object> dictionary |
51 | 9c4346c9 | Panagiotis Kanavos | = new Dictionary<string, object>(); |
52 | 9c4346c9 | Panagiotis Kanavos | |
53 | 9c4346c9 | Panagiotis Kanavos | // This property returns the number of elements |
54 | 9c4346c9 | Panagiotis Kanavos | // in the inner dictionary. |
55 | 9c4346c9 | Panagiotis Kanavos | public int Count |
56 | 9c4346c9 | Panagiotis Kanavos | { |
57 | 9c4346c9 | Panagiotis Kanavos | get |
58 | 9c4346c9 | Panagiotis Kanavos | { |
59 | 9c4346c9 | Panagiotis Kanavos | return dictionary.Count; |
60 | 9c4346c9 | Panagiotis Kanavos | } |
61 | 9c4346c9 | Panagiotis Kanavos | } |
62 | 9c4346c9 | Panagiotis Kanavos | |
63 | 9c4346c9 | Panagiotis Kanavos | // If you try to get a value of a property |
64 | 9c4346c9 | Panagiotis Kanavos | // not defined in the class, this method is called. |
65 | 9c4346c9 | Panagiotis Kanavos | public override bool TryGetMember( |
66 | 9c4346c9 | Panagiotis Kanavos | GetMemberBinder binder, out object result) |
67 | 9c4346c9 | Panagiotis Kanavos | { |
68 | 9c4346c9 | Panagiotis Kanavos | // Converting the property name to lowercase |
69 | 9c4346c9 | Panagiotis Kanavos | // so that property names become case-insensitive. |
70 | 9c4346c9 | Panagiotis Kanavos | string name = binder.Name.ToLower(); |
71 | 9c4346c9 | Panagiotis Kanavos | |
72 | 9c4346c9 | Panagiotis Kanavos | // If the property name is found in a dictionary, |
73 | 9c4346c9 | Panagiotis Kanavos | // set the result parameter to the property value and return true. |
74 | 9c4346c9 | Panagiotis Kanavos | // Otherwise, return false. |
75 | 9c4346c9 | Panagiotis Kanavos | return dictionary.TryGetValue(name, out result); |
76 | 9c4346c9 | Panagiotis Kanavos | } |
77 | 9c4346c9 | Panagiotis Kanavos | |
78 | 9c4346c9 | Panagiotis Kanavos | // If you try to set a value of a property that is |
79 | 9c4346c9 | Panagiotis Kanavos | // not defined in the class, this method is called. |
80 | 9c4346c9 | Panagiotis Kanavos | public override bool TrySetMember( |
81 | 9c4346c9 | Panagiotis Kanavos | SetMemberBinder binder, object value) |
82 | 9c4346c9 | Panagiotis Kanavos | { |
83 | 9c4346c9 | Panagiotis Kanavos | // Converting the property name to lowercase |
84 | 9c4346c9 | Panagiotis Kanavos | // so that property names become case-insensitive. |
85 | 9c4346c9 | Panagiotis Kanavos | dictionary[binder.Name.ToLower()] = value; |
86 | 9c4346c9 | Panagiotis Kanavos | |
87 | 9c4346c9 | Panagiotis Kanavos | // You can always add a value to a dictionary, |
88 | 9c4346c9 | Panagiotis Kanavos | // so this method always returns true. |
89 | 9c4346c9 | Panagiotis Kanavos | return true; |
90 | 9c4346c9 | Panagiotis Kanavos | } |
91 | 9c4346c9 | Panagiotis Kanavos | } |
92 | 9c4346c9 | Panagiotis Kanavos | } |