Changeset 1316
- Timestamp:
- 03/09/09 15:16:14 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/CompoundSerializers.cs
r1314 r1316 37 37 38 38 public class ArraySerializer : ICompoundSerializer { 39 40 39 public bool CanSerialize(Type type) { 41 40 return type.IsArray; 42 41 } 43 44 42 public IEnumerable Serialize(object array) { 45 43 Array a = (Array)array; … … 52 50 } 53 51 } 54 55 52 public object DeSerialize(IEnumerable elements, Type t) { 56 53 IEnumerator e = elements.GetEnumerator(); … … 78 75 return a; 79 76 } 80 81 77 } 82 78 79 public class KeyValuePair2XmlSerializer : ICompoundSerializer { 80 public bool CanSerialize(Type type) { 81 return type.IsGenericType && 82 type.GetGenericTypeDefinition().Name == "KeyValuePair`2"; 83 } 84 public IEnumerable Serialize(object o) { 85 Type t = o.GetType(); 86 yield return t.GetProperty("Key").GetValue(o, null); 87 yield return t.GetProperty("Value").GetValue(o, null); 88 } 89 public object DeSerialize(IEnumerable o, Type t) { 90 return Activator.CreateInstance(t, 91 new List<object>((IEnumerable<object>)o).ToArray()); 92 } 93 } 94 95 public class Dictionary2XmlSerializer : ICompoundSerializer { 96 public bool CanSerialize(Type type) { 97 return type.GetInterface("IDictionary") != null; 98 } 99 public IEnumerable Serialize(object o) { 100 IDictionary dict = (IDictionary)o; 101 foreach ( DictionaryEntry entry in dict) { 102 yield return entry.Key; 103 yield return entry.Value; 104 } 105 } 106 public object DeSerialize(IEnumerable o, Type t) { 107 IDictionary dict = (IDictionary)Activator.CreateInstance(t); 108 IEnumerator iter = o.GetEnumerator(); 109 while (iter.MoveNext()) { 110 object key = iter.Current; 111 iter.MoveNext(); 112 object value = iter.Current; 113 dict.Add(key, value); 114 } 115 return dict; 116 } 117 } 83 118 84 119 }
Note: See TracChangeset
for help on using the changeset viewer.