Mercurial > pub > bltoolkit
comparison Source/Mapping/DictionaryMapper.cs @ 0:f990fcb411a9
Копия текущей версии из github
| author | cin |
|---|---|
| date | Thu, 27 Mar 2014 21:46:09 +0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:f990fcb411a9 |
|---|---|
| 1 using System; | |
| 2 using System.Collections; | |
| 3 using System.Collections.Generic; | |
| 4 | |
| 5 namespace BLToolkit.Mapping | |
| 6 { | |
| 7 public class DictionaryMapper : MapDataSourceDestinationBase | |
| 8 { | |
| 9 public DictionaryMapper(IDictionary dictionary) | |
| 10 { | |
| 11 if (dictionary == null) throw new ArgumentNullException("dictionary"); | |
| 12 | |
| 13 _dictionary = dictionary; | |
| 14 } | |
| 15 | |
| 16 private readonly IDictionary _dictionary; | |
| 17 public IDictionary Dictionary | |
| 18 { | |
| 19 get { return _dictionary; } | |
| 20 } | |
| 21 | |
| 22 private int _currentIndex; | |
| 23 private IDictionaryEnumerator _enumerator; | |
| 24 | |
| 25 private void SetEnumerator(int i) | |
| 26 { | |
| 27 if (_enumerator == null) | |
| 28 { | |
| 29 _enumerator = _dictionary.GetEnumerator(); | |
| 30 _enumerator.MoveNext(); | |
| 31 } | |
| 32 | |
| 33 if (_currentIndex > i) | |
| 34 { | |
| 35 _currentIndex = 0; | |
| 36 _enumerator.Reset(); | |
| 37 _enumerator.MoveNext(); | |
| 38 } | |
| 39 | |
| 40 for (; _currentIndex < i; _currentIndex++) | |
| 41 _enumerator.MoveNext(); | |
| 42 } | |
| 43 | |
| 44 #region IMapDataSource Members | |
| 45 | |
| 46 public override int Count | |
| 47 { | |
| 48 get { return _dictionary.Count; } | |
| 49 } | |
| 50 | |
| 51 public override Type GetFieldType(int index) | |
| 52 { | |
| 53 SetEnumerator(index); | |
| 54 return _enumerator.Value == null? typeof(object): _enumerator.Value.GetType(); | |
| 55 } | |
| 56 | |
| 57 public override string GetName(int index) | |
| 58 { | |
| 59 SetEnumerator(index); | |
| 60 return _enumerator.Key.ToString(); | |
| 61 } | |
| 62 | |
| 63 public override bool SupportsTypedValues(int index) | |
| 64 { | |
| 65 return index < _dictionary.Count; | |
| 66 } | |
| 67 | |
| 68 public override object GetValue(object o, int index) | |
| 69 { | |
| 70 SetEnumerator(index); | |
| 71 return _enumerator.Value; | |
| 72 } | |
| 73 | |
| 74 public override object GetValue(object o, string name) | |
| 75 { | |
| 76 return _dictionary.Contains(name) ? _dictionary[name] : null; | |
| 77 } | |
| 78 | |
| 79 #endregion | |
| 80 | |
| 81 #region IMapDataDestination Members | |
| 82 | |
| 83 private List<string> _nameList; | |
| 84 | |
| 85 public override int GetOrdinal(string name) | |
| 86 { | |
| 87 if (_nameList == null) | |
| 88 _nameList = new List<string>(); | |
| 89 | |
| 90 var idx = _nameList.IndexOf(name); | |
| 91 | |
| 92 if (idx >= 0) | |
| 93 return idx; | |
| 94 | |
| 95 _nameList.Add(name); | |
| 96 | |
| 97 return _nameList.Count - 1; | |
| 98 } | |
| 99 | |
| 100 public override void SetValue(object o, int index, object value) | |
| 101 { | |
| 102 SetValue(o, _nameList[index], value); | |
| 103 } | |
| 104 | |
| 105 public override void SetValue(object o, string name, object value) | |
| 106 { | |
| 107 if (_dictionary.Contains(name)) | |
| 108 _dictionary[name] = value; | |
| 109 else | |
| 110 _dictionary.Add(name, value); | |
| 111 } | |
| 112 | |
| 113 #endregion | |
| 114 } | |
| 115 } |
