Changeset 1313
- Timestamp:
- 03/09/09 12:42:49 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/CompoundSerializers.cs
r1280 r1313 38 38 public class ArraySerializer : ICustomSerializer { 39 39 40 public bool CanSerialize(Type type) { 40 public bool CanSerialize(Type type) { 41 41 return type.IsArray; 42 42 } 43 43 44 44 public IEnumerable Serialize(object array) { 45 Array a = (Array)array; 46 yield return a.Rank; 47 for (int i = 0; i < a.Rank; i++) { 48 yield return a.GetLength(i); 49 } 45 50 foreach (object o in (Array)array) { 46 51 yield return o; … … 49 54 50 55 public object DeSerialize(IEnumerable elements, Type t) { 51 List<object> allElements = new List<object>(); 52 foreach (object obj in elements) { 53 allElements.Add(obj); 56 IEnumerator e = elements.GetEnumerator(); 57 e.MoveNext(); 58 int rank = (int)e.Current; 59 int[] lengths = new int[rank]; 60 for (int i = 0; i < rank; i++) { 61 e.MoveNext(); 62 lengths[i] = (int)e.Current; 54 63 } 55 Array array = 56 Array.CreateInstance(t.GetElementType(), allElements.Count); 57 for (int i = 0; i < array.Length; i++) { 58 array.SetValue(allElements[i], i); 64 Array a = Array.CreateInstance(t.GetElementType(), lengths); 65 int[] positions = new int[rank]; 66 while (e.MoveNext()) { 67 a.SetValue(e.Current, positions); 68 positions[0] += 1; 69 for (int i = 0; i < rank-1; i++) { 70 if (positions[i] >= lengths[i]) { 71 positions[i] = 0; 72 positions[i + 1] += 1; 73 } else { 74 break; 75 } 76 } 59 77 } 60 return a rray;78 return a; 61 79 } 62 80
Note: See TracChangeset
for help on using the changeset viewer.