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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
//-----------------------------------------------------------------------
// <copyright file="MessageDictionary.cs" company="Andrew Arnott">
// Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOAuth.Messaging.Reflection {
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
/// <summary>
/// Wraps an <see cref="IProtocolMessage"/> instance in a dictionary that
/// provides access to both well-defined message properties and "extra"
/// name/value pairs that have no properties associated with them.
/// </summary>
internal class MessageDictionary : IDictionary<string, string> {
private IProtocolMessage message;
private MessageDescription description;
internal MessageDictionary(IProtocolMessage message) {
if (message == null) {
throw new ArgumentNullException("message");
}
this.message = message;
this.description = MessageDescription.Get(message.GetType());
}
#region IDictionary<string,string> Members
public void Add(string key, string value) {
if (value == null) {
throw new ArgumentNullException("value");
}
MessagePart part;
if (this.description.Mapping.TryGetValue(key, out part)) {
if (part.IsNondefaultValueSet(this.message)) {
throw new ArgumentException(MessagingStrings.KeyAlreadyExists);
}
part.SetValue(this.message, value);
} else {
this.message.ExtraData.Add(key, value);
}
}
public bool ContainsKey(string key) {
return this.message.ExtraData.ContainsKey(key) ||
(this.description.Mapping.ContainsKey(key) && this.description.Mapping[key].GetValue(this.message) != null);
}
public ICollection<string> Keys {
get {
List<string> keys = new List<string>(this.message.ExtraData.Count + this.description.Mapping.Count);
foreach (var pair in this.description.Mapping) {
// Don't include keys with null values, but default values for structs is ok
if (pair.Value.GetValue(this.message) != null) {
keys.Add(pair.Key);
}
}
foreach (string key in this.message.ExtraData.Keys) {
keys.Add(key);
}
return keys.AsReadOnly();
}
}
public bool Remove(string key) {
if (this.message.ExtraData.Remove(key)) {
return true;
} else {
MessagePart part;
if (this.description.Mapping.TryGetValue(key, out part)) {
if (part.GetValue(this.message) != null) {
part.SetValue(this.message, null);
return true;
}
}
return false;
}
}
public bool TryGetValue(string key, out string value) {
MessagePart part;
if (this.description.Mapping.TryGetValue(key, out part)) {
value = part.GetValue(this.message);
return true;
}
return this.message.ExtraData.TryGetValue(key, out value);
}
public ICollection<string> Values {
get {
List<string> values = new List<string>(this.message.ExtraData.Count + this.description.Mapping.Count);
foreach (MessagePart part in this.description.Mapping.Values) {
if (part.GetValue(this.message) != null) {
values.Add(part.GetValue(this.message));
}
}
foreach (string value in this.message.ExtraData.Values) {
Debug.Assert(value != null, "Null values should never be allowed in the extra data dictionary.");
values.Add(value);
}
return values.AsReadOnly();
}
}
public string this[string key] {
get {
MessagePart part;
if (this.description.Mapping.TryGetValue(key, out part)) {
// Never throw KeyNotFoundException for declared properties.
return part.GetValue(this.message);
} else {
return this.message.ExtraData[key];
}
}
set {
MessagePart part;
if (this.description.Mapping.TryGetValue(key, out part)) {
part.SetValue(this.message, value);
} else {
if (value == null) {
this.message.ExtraData.Remove(key);
} else {
this.message.ExtraData[key] = value;
}
}
}
}
#endregion
#region ICollection<KeyValuePair<string,string>> Members
public void Add(KeyValuePair<string, string> item) {
this.Add(item.Key, item.Value);
}
public void Clear() {
foreach (string key in this.Keys) {
this.Remove(key);
}
}
public bool Contains(KeyValuePair<string, string> item) {
MessagePart part;
if (this.description.Mapping.TryGetValue(item.Key, out part)) {
return string.Equals(part.GetValue(this.message), item.Value, StringComparison.Ordinal);
} else {
return this.message.ExtraData.Contains(item);
}
}
void ICollection<KeyValuePair<string, string>>.CopyTo(KeyValuePair<string, string>[] array, int arrayIndex) {
foreach (var pair in (IDictionary<string, string>)this) {
array[arrayIndex++] = pair;
}
}
public int Count {
get { return this.Keys.Count; }
}
bool ICollection<KeyValuePair<string, string>>.IsReadOnly {
get { return false; }
}
public bool Remove(KeyValuePair<string, string> item) {
// We use contains because that checks that the value is equal as well.
if (((ICollection<KeyValuePair<string, string>>)this).Contains(item)) {
((IDictionary<string, string>)this).Remove(item.Key);
return true;
}
return false;
}
#endregion
#region IEnumerable<KeyValuePair<string,string>> Members
public IEnumerator<KeyValuePair<string, string>> GetEnumerator() {
foreach (string key in Keys) {
yield return new KeyValuePair<string, string>(key, this[key]);
}
}
#endregion
#region IEnumerable Members
IEnumerator System.Collections.IEnumerable.GetEnumerator() {
return ((IEnumerable<KeyValuePair<string, string>>)this).GetEnumerator();
}
#endregion
}
}
|