1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\r
5 * This software is subject to the Microsoft Public License (Ms-PL).
\r
6 * A copy of the license can be found in the license.htm file included
\r
7 * in this distribution.
\r
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\r
15 using System.Collections;
\r
16 using System.Collections.Generic;
\r
17 using System.Globalization;
\r
18 using System.Web.Mvc.Resources;
\r
20 public class ModelBinderDictionary : IDictionary<Type, IModelBinder> {
\r
22 private IModelBinder _defaultBinder;
\r
23 private readonly Dictionary<Type, IModelBinder> _innerDictionary = new Dictionary<Type, IModelBinder>();
\r
27 return _innerDictionary.Count;
\r
31 public IModelBinder DefaultBinder {
\r
33 if (_defaultBinder == null) {
\r
34 _defaultBinder = new DefaultModelBinder();
\r
36 return _defaultBinder;
\r
39 _defaultBinder = value;
\r
43 public bool IsReadOnly {
\r
45 return ((IDictionary<Type, IModelBinder>)_innerDictionary).IsReadOnly;
\r
49 public ICollection<Type> Keys {
\r
51 return _innerDictionary.Keys;
\r
55 public IModelBinder this[Type key] {
\r
57 IModelBinder binder;
\r
58 _innerDictionary.TryGetValue(key, out binder);
\r
62 _innerDictionary[key] = value;
\r
66 public ICollection<IModelBinder> Values {
\r
68 return _innerDictionary.Values;
\r
72 public void Add(KeyValuePair<Type, IModelBinder> item) {
\r
73 ((IDictionary<Type, IModelBinder>)_innerDictionary).Add(item);
\r
76 public void Add(Type key, IModelBinder value) {
\r
77 _innerDictionary.Add(key, value);
\r
80 public void Clear() {
\r
81 _innerDictionary.Clear();
\r
84 public bool Contains(KeyValuePair<Type, IModelBinder> item) {
\r
85 return ((IDictionary<Type, IModelBinder>)_innerDictionary).Contains(item);
\r
88 public bool ContainsKey(Type key) {
\r
89 return _innerDictionary.ContainsKey(key);
\r
92 public void CopyTo(KeyValuePair<Type, IModelBinder>[] array, int arrayIndex) {
\r
93 ((IDictionary<Type, IModelBinder>)_innerDictionary).CopyTo(array, arrayIndex);
\r
96 public IModelBinder GetBinder(Type modelType) {
\r
97 return GetBinder(modelType, true /* fallbackToDefault */);
\r
100 public virtual IModelBinder GetBinder(Type modelType, bool fallbackToDefault) {
\r
101 if (modelType == null) {
\r
102 throw new ArgumentNullException("modelType");
\r
105 return GetBinder(modelType, (fallbackToDefault) ? DefaultBinder : null);
\r
108 private IModelBinder GetBinder(Type modelType, IModelBinder fallbackBinder) {
\r
109 // Try to look up a binder for this type. We use this order of precedence:
\r
110 // 1. Binder registered in the global table
\r
111 // 2. Binder attribute defined on the type
\r
112 // 3. Supplied fallback binder
\r
114 IModelBinder binder;
\r
115 if (_innerDictionary.TryGetValue(modelType, out binder)) {
\r
119 binder = ModelBinders.GetBinderFromAttributes(modelType,
\r
120 () => String.Format(CultureInfo.CurrentUICulture, MvcResources.ModelBinderDictionary_MultipleAttributes, modelType.FullName));
\r
122 return binder ?? fallbackBinder;
\r
125 public IEnumerator<KeyValuePair<Type, IModelBinder>> GetEnumerator() {
\r
126 return _innerDictionary.GetEnumerator();
\r
129 public bool Remove(KeyValuePair<Type, IModelBinder> item) {
\r
130 return ((IDictionary<Type, IModelBinder>)_innerDictionary).Remove(item);
\r
133 public bool Remove(Type key) {
\r
134 return _innerDictionary.Remove(key);
\r
137 public bool TryGetValue(Type key, out IModelBinder value) {
\r
138 return _innerDictionary.TryGetValue(key, out value);
\r
141 #region IEnumerable Members
\r
142 IEnumerator IEnumerable.GetEnumerator() {
\r
143 return ((IEnumerable)_innerDictionary).GetEnumerator();
\r