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.Generic;
\r
16 using System.Globalization;
\r
18 public class DictionaryValueProvider<TValue> : IValueProvider {
\r
20 private readonly HashSet<string> _prefixes = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
\r
21 private readonly Dictionary<string, ValueProviderResult> _values = new Dictionary<string, ValueProviderResult>(StringComparer.OrdinalIgnoreCase);
\r
23 public DictionaryValueProvider(IDictionary<string, TValue> dictionary, CultureInfo culture) {
\r
24 if (dictionary == null) {
\r
25 throw new ArgumentNullException("dictionary");
\r
28 AddValues(dictionary, culture);
\r
31 private void AddValues(IDictionary<string, TValue> dictionary, CultureInfo culture) {
\r
32 if (dictionary.Count > 0) {
\r
36 foreach (var entry in dictionary) {
\r
37 _prefixes.UnionWith(ValueProviderUtil.GetPrefixes(entry.Key));
\r
39 object rawValue = entry.Value;
\r
40 string attemptedValue = Convert.ToString(rawValue, culture);
\r
41 _values[entry.Key] = new ValueProviderResult(rawValue, attemptedValue, culture);
\r
45 public virtual bool ContainsPrefix(string prefix) {
\r
46 if (prefix == null) {
\r
47 throw new ArgumentNullException("prefix");
\r
50 return _prefixes.Contains(prefix);
\r
53 public virtual ValueProviderResult GetValue(string key) {
\r
55 throw new ArgumentNullException("key");
\r
58 ValueProviderResult vpResult;
\r
59 _values.TryGetValue(key, out vpResult);
\r