1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Web.WebPages.Resources;
namespace System.Web.WebPages
{
/// <summary>
/// This is a wrapper around PageDataDictionary[[dynamic]] which allows dynamic
/// access (e.g. dict.Foo). Like PageDataDictionary, it returns null if the key is not found,
/// instead of throwing an exception.
/// This class is intended to be used as DynamicPageDataDictionary[[dynamic]]
/// </summary>
// This is a generic type because C# does not allow implementing an interface
// involving dynamic types (implementing IDictionary<object, dynamic> causes
// a compile error
// http://blogs.msdn.com/cburrows/archive/2009/02/04/c-dynamic-part-vii.aspx).
internal class DynamicPageDataDictionary<TValue> : DynamicObject, IDictionary<object, TValue>
{
private PageDataDictionary<TValue> _data;
public DynamicPageDataDictionary(PageDataDictionary<TValue> dictionary)
{
_data = dictionary;
}
public ICollection<object> Keys
{
get { return _data.Keys; }
}
public ICollection<TValue> Values
{
get { return _data.Values; }
}
public int Count
{
get { return _data.Count; }
}
public bool IsReadOnly
{
get { return _data.IsReadOnly; }
}
public TValue this[object key]
{
get { return _data[key]; }
set { _data[key] = value; }
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = _data[binder.Name];
// We return true here because PageDataDictionary returns null if the key is not
// in the dictionary, so we simply pass on the returned value.
return true;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
// This cast should always succeed assuming TValue is dynamic.
TValue v = (TValue)value;
_data[binder.Name] = v;
return true;
}
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
{
if (indexes == null || indexes.Length != 1)
{
throw new ArgumentException(WebPageResources.DynamicDictionary_InvalidNumberOfIndexes);
}
result = _data[indexes[0]];
// We return true here because PageDataDictionary returns null if the key is not
// in the dictionary, so we simply pass on the returned value.
return true;
}
public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value)
{
if (indexes == null || indexes.Length != 1)
{
throw new ArgumentException(WebPageResources.DynamicDictionary_InvalidNumberOfIndexes);
}
// This cast should always succeed assuming TValue is dynamic.
_data[indexes[0]] = (TValue)value;
return true;
}
public void Add(object key, TValue value)
{
_data.Add(key, value);
}
public bool ContainsKey(object key)
{
return _data.ContainsKey(key);
}
public bool Remove(object key)
{
return _data.Remove(key);
}
public bool TryGetValue(object key, out TValue value)
{
return _data.TryGetValue(key, out value);
}
public void Add(KeyValuePair<object, TValue> item)
{
_data.Add(item);
}
public void Clear()
{
_data.Clear();
}
public bool Contains(KeyValuePair<object, TValue> item)
{
return _data.Contains(item);
}
public void CopyTo(KeyValuePair<object, TValue>[] array, int arrayIndex)
{
_data.CopyTo(array, arrayIndex);
}
public bool Remove(KeyValuePair<object, TValue> item)
{
return _data.Remove(item.Key);
}
public IEnumerator<KeyValuePair<object, TValue>> GetEnumerator()
{
return _data.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _data.GetEnumerator();
}
}
}
|