2 // System.Runtime.Remoting.Messaging.MethodDictionary.cs
4 // Author: Lluis Sanchez Gual (lluis@ideary.com)
6 // 2003 (C) Lluis Sanchez Gual
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
35 namespace System.Runtime.Remoting.Messaging
38 internal class MessageDictionary : IDictionary
40 IDictionary _internalProperties = null;
41 protected IMethodMessage _message;
43 bool _ownProperties = false;
45 public MessageDictionary (IMethodMessage message)
50 internal bool HasUserData ()
52 if (null != _internalProperties)
54 // MethodCallMessageWrapper uses a nested MessageDictionary
55 if (_internalProperties is MessageDictionary)
56 return ((MessageDictionary)_internalProperties).HasUserData ();
58 return _internalProperties.Count > 0;
63 internal IDictionary InternalDictionary
67 if (null != _internalProperties)
69 if (_internalProperties is MessageDictionary)
70 return ((MessageDictionary)_internalProperties).InternalDictionary;
72 return _internalProperties;
76 public string[] MethodKeys
78 get { return _methodKeys; }
79 set { _methodKeys = value; }
82 protected virtual IDictionary AllocInternalProperties()
84 _ownProperties = true;
85 return new Hashtable();
88 public IDictionary GetInternalProperties()
90 if (_internalProperties == null) _internalProperties = AllocInternalProperties();
91 return _internalProperties;
94 private bool IsOverridenKey (string key)
96 // Small optimization. If the internal properties have been
97 // created by this dictionary, then it can be assured that it does
98 // not contain values for overriden keys.
99 if (_ownProperties) return false;
101 foreach (string mkey in _methodKeys)
102 if (key == mkey) return true;
106 public MessageDictionary(string[] keys)
111 public bool IsFixedSize
113 get { return false; }
116 public bool IsReadOnly
118 get { return false; }
121 public object this[object key]
125 string keyStr = (string)key;
126 for (int n=0; n<_methodKeys.Length; n++)
127 if (_methodKeys[n] == keyStr) return GetMethodProperty (keyStr);
129 if (_internalProperties != null)
130 return _internalProperties[key];
141 protected virtual object GetMethodProperty (string key)
145 case "__Uri" : return _message.Uri;
146 case "__MethodName" : return _message.MethodName;
147 case "__TypeName" : return _message.TypeName;
148 case "__MethodSignature" : return _message.MethodSignature;
149 case "__CallContext" : return _message.LogicalCallContext;
150 case "__Args" : return _message.Args;
151 case "__OutArgs": return ((IMethodReturnMessage)_message).OutArgs;
152 case "__Return": return ((IMethodReturnMessage)_message).ReturnValue;
153 default : return null;
157 protected virtual void SetMethodProperty (string key, object value)
161 case "__CallContext": // Ignore?
163 case "__Return": return;
165 case "__MethodName" :
167 case "__MethodSignature" :
168 case "__Args" : return; //throw new ArgumentException ("key was invalid " + key);
169 case "__Uri": ((IInternalMessage)_message).Uri = (string) value; return;
173 public ICollection Keys
177 ArrayList keys = new ArrayList();
178 for (int n=0; n<_methodKeys.Length; n++)
179 keys.Add (_methodKeys[n]);
181 if (_internalProperties != null)
183 foreach (string key in _internalProperties.Keys)
184 if (!IsOverridenKey (key)) keys.Add (key);
191 public ICollection Values
195 ArrayList values = new ArrayList();
196 for (int n=0; n<_methodKeys.Length; n++)
197 values.Add (GetMethodProperty(_methodKeys[n]));
199 if (_internalProperties != null)
201 foreach (DictionaryEntry entry in _internalProperties)
202 if (!IsOverridenKey((string)entry.Key)) values.Add (entry.Value);
209 public void Add (object key, object value)
211 string keyStr = (string)key;
212 for (int n=0; n<_methodKeys.Length; n++)
213 if (_methodKeys[n] == keyStr) {
214 SetMethodProperty (keyStr, value);
218 if (_internalProperties == null) _internalProperties = AllocInternalProperties();
219 _internalProperties[key] = value;
224 if (_internalProperties != null) _internalProperties.Clear();
227 public bool Contains (object key)
229 string keyStr = (string)key;
230 for (int n=0; n<_methodKeys.Length; n++)
231 if (_methodKeys[n] == keyStr) return true;
233 if (_internalProperties != null) return _internalProperties.Contains (key);
237 public void Remove (object key)
239 string keyStr = (string)key;
240 for (int n=0; n<_methodKeys.Length; n++)
241 if (_methodKeys[n] == keyStr) throw new ArgumentException ("key was invalid");
243 if (_internalProperties != null) _internalProperties.Remove (key);
250 if (_internalProperties != null) return _internalProperties.Count + _methodKeys.Length;
251 else return _methodKeys.Length;
255 public bool IsSynchronized
257 get { return false; }
260 public object SyncRoot
265 public void CopyTo (Array array, int index)
267 Values.CopyTo (array, index);
270 IEnumerator IEnumerable.GetEnumerator()
272 return new DictionaryEnumerator (this);
275 public IDictionaryEnumerator GetEnumerator ()
277 return new DictionaryEnumerator (this);
280 // Dictionary enumerator
282 class DictionaryEnumerator : IDictionaryEnumerator
284 MessageDictionary _methodDictionary;
285 IDictionaryEnumerator _hashtableEnum;
288 public DictionaryEnumerator (MessageDictionary methodDictionary)
290 _methodDictionary = methodDictionary;
291 _hashtableEnum = (_methodDictionary._internalProperties != null) ? _methodDictionary._internalProperties.GetEnumerator() : null;
295 public object Current
300 public bool MoveNext()
302 if (_posMethod != -2)
305 if (_posMethod < _methodDictionary._methodKeys.Length) return true;
309 if (_hashtableEnum == null) return false;
311 while (_hashtableEnum.MoveNext())
313 if (!_methodDictionary.IsOverridenKey((string)_hashtableEnum.Key))
322 _hashtableEnum.Reset();
325 public DictionaryEntry Entry
330 return new DictionaryEntry (_methodDictionary._methodKeys[_posMethod], _methodDictionary.GetMethodProperty(_methodDictionary._methodKeys[_posMethod]));
331 else if (_posMethod == -1 || _hashtableEnum == null)
332 throw new InvalidOperationException ("The enumerator is positioned before the first element of the collection or after the last element");
334 return _hashtableEnum.Entry;
340 get { return Entry.Key; }
345 get { return Entry.Value; }